1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2005 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>
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_access.h>
33 #include <sys/ioctl.h>
36 #include <sys/types.h>
43 #include <vlc_charset.h>
45 /* DVB Card Drivers */
46 #include <linux/dvb/version.h>
47 #include <linux/dvb/dmx.h>
48 #include <linux/dvb/frontend.h>
49 #include <linux/dvb/ca.h>
51 /* Include dvbpsi headers */
52 #ifdef HAVE_DVBPSI_DR_H
53 # include <dvbpsi/dvbpsi.h>
54 # include <dvbpsi/descriptor.h>
55 # include <dvbpsi/pat.h>
56 # include <dvbpsi/pmt.h>
57 # include <dvbpsi/dr.h>
58 # include <dvbpsi/psi.h>
59 # include <dvbpsi/demux.h>
60 # include <dvbpsi/sdt.h>
63 # include "descriptor.h"
64 # include "tables/pat.h"
65 # include "tables/pmt.h"
66 # include "descriptors/dr.h"
69 # include "tables/sdt.h"
73 # include <vlc_httpd.h>
83 fe_status_t i_last_status;
84 struct dvb_frontend_info info;
87 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
89 /* Local prototypes */
90 static int FrontendInfo( access_t * );
91 static int FrontendSetQPSK( access_t * );
92 static int FrontendSetQAM( access_t * );
93 static int FrontendSetOFDM( access_t * );
94 static int FrontendSetATSC( access_t * );
96 /*****************************************************************************
97 * FrontendOpen : Determine frontend device information and capabilities
98 *****************************************************************************/
99 int FrontendOpen( access_t *p_access )
101 access_sys_t *p_sys = p_access->p_sys;
102 frontend_t * p_frontend;
103 unsigned int i_adapter, i_device;
107 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
108 i_device = var_GetInteger( p_access, "dvb-device" );
109 b_probe = var_GetBool( p_access, "dvb-probe" );
111 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
113 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
114 frontend[sizeof(frontend) - 1] = '\0';
117 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
121 msg_Dbg( p_access, "Opening device %s", frontend );
122 if( (p_sys->i_frontend_handle = utf8_open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
124 msg_Err( p_access, "FrontEndOpen: opening device failed (%m)" );
131 const char * psz_expected = NULL;
132 const char * psz_real;
134 if( FrontendInfo( p_access ) < 0 )
136 close( p_sys->i_frontend_handle );
141 switch( p_frontend->info.type )
156 psz_real = "unknown";
160 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
161 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
162 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
163 (p_frontend->info.type != FE_QPSK) )
165 psz_expected = "DVB-S";
167 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
168 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
169 (p_frontend->info.type != FE_QAM) )
171 psz_expected = "DVB-C";
173 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
174 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
175 (p_frontend->info.type != FE_OFDM) )
177 psz_expected = "DVB-T";
180 if( (!strncmp( p_access->psz_access, "usdigital", 9 ) ||
181 !strncmp( p_access->psz_access, "atsc", 4 ) ) &&
182 (p_frontend->info.type != FE_ATSC) )
184 psz_expected = "ATSC";
187 if( psz_expected != NULL )
189 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
190 psz_expected, psz_real );
191 close( p_sys->i_frontend_handle );
196 else /* no frontend probing is done so use default border values. */
198 msg_Dbg( p_access, "using default values for frontend info" );
200 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
201 p_frontend->info.type = FE_QPSK;
202 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
203 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
204 p_frontend->info.type = FE_QPSK;
205 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
206 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
207 p_frontend->info.type = FE_QAM;
208 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
209 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
210 p_frontend->info.type = FE_OFDM;
211 else if( !strncmp( p_access->psz_access, "usdigital", 9 ) ||
212 !strncmp( p_access->psz_access, "atsc", 4 ) )
213 p_frontend->info.type = FE_ATSC;
219 /*****************************************************************************
220 * FrontendClose : Close the frontend
221 *****************************************************************************/
222 void FrontendClose( access_t *p_access )
224 access_sys_t *p_sys = p_access->p_sys;
226 if( p_sys->p_frontend )
228 close( p_sys->i_frontend_handle );
229 free( p_sys->p_frontend );
231 p_sys->p_frontend = NULL;
235 /*****************************************************************************
236 * FrontendSet : Tune !
237 *****************************************************************************/
238 int FrontendSet( access_t *p_access )
240 access_sys_t *p_sys = p_access->p_sys;
242 switch( p_sys->p_frontend->info.type )
246 if( FrontendSetQPSK( p_access ) < 0 )
248 msg_Err( p_access, "DVB-S: tuning failed" );
255 if( FrontendSetQAM( p_access ) < 0 )
257 msg_Err( p_access, "DVB-C: tuning failed" );
264 if( FrontendSetOFDM( p_access ) < 0 )
266 msg_Err( p_access, "DVB-T: tuning failed" );
273 if( FrontendSetATSC( p_access ) < 0 )
275 msg_Err( p_access, "ATSC: tuning failed" );
281 msg_Err( p_access, "Could not determine frontend type on %s",
282 p_sys->p_frontend->info.name );
285 p_sys->p_frontend->i_last_status = 0;
286 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
290 /*****************************************************************************
291 * FrontendPoll : Poll for frontend events
292 *****************************************************************************/
293 void FrontendPoll( access_t *p_access )
295 access_sys_t *p_sys = p_access->p_sys;
296 frontend_t * p_frontend = p_sys->p_frontend;
297 struct dvb_frontend_event event;
298 fe_status_t i_status, i_diff;
302 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
306 if( errno == EWOULDBLOCK )
307 return; /* no more events */
309 msg_Err( p_access, "reading frontend event failed (%d): %m",
314 i_status = event.status;
315 i_diff = i_status ^ p_frontend->i_last_status;
316 p_frontend->i_last_status = i_status;
321 if ( i_diff & (x) ) \
323 if ( i_status & (x) )
325 IF_UP( FE_HAS_SIGNAL )
326 msg_Dbg( p_access, "frontend has acquired signal" );
328 msg_Dbg( p_access, "frontend has lost signal" );
330 IF_UP( FE_HAS_CARRIER )
331 msg_Dbg( p_access, "frontend has acquired carrier" );
333 msg_Dbg( p_access, "frontend has lost carrier" );
335 IF_UP( FE_HAS_VITERBI )
336 msg_Dbg( p_access, "frontend has acquired stable FEC" );
338 msg_Dbg( p_access, "frontend has lost FEC" );
341 msg_Dbg( p_access, "frontend has acquired sync" );
343 msg_Dbg( p_access, "frontend has lost sync" );
347 frontend_statistic_t stat;
349 msg_Dbg( p_access, "frontend has acquired lock" );
350 p_sys->i_frontend_timeout = 0;
352 /* Read some statistics */
353 if( !FrontendGetStatistic( p_access, &stat ) )
355 if( stat.i_ber >= 0 )
356 msg_Dbg( p_access, "- Bit error rate: %d", stat.i_ber );
357 if( stat.i_signal_strenth >= 0 )
358 msg_Dbg( p_access, "- Signal strength: %d", stat.i_signal_strenth );
359 if( stat.i_snr >= 0 )
360 msg_Dbg( p_access, "- SNR: %d", stat.i_snr );
365 msg_Dbg( p_access, "frontend has lost lock" );
366 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
371 /* The frontend was reinited. */
372 msg_Warn( p_access, "reiniting frontend");
373 FrontendSet( p_access );
379 int FrontendGetStatistic( access_t *p_access, frontend_statistic_t *p_stat )
381 access_sys_t *p_sys = p_access->p_sys;
382 frontend_t * p_frontend = p_sys->p_frontend;
384 if( (p_frontend->i_last_status & FE_HAS_LOCK) == 0 )
387 memset( p_stat, 0, sizeof(*p_stat) );
388 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &p_stat->i_ber ) < 0 )
390 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &p_stat->i_signal_strenth ) < 0 )
391 p_stat->i_signal_strenth = -1;
392 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &p_stat->i_snr ) < 0 )
397 void FrontendGetStatus( access_t *p_access, frontend_status_t *p_status )
399 access_sys_t *p_sys = p_access->p_sys;
400 frontend_t * p_frontend = p_sys->p_frontend;
402 p_status->b_has_signal = (p_frontend->i_last_status & FE_HAS_SIGNAL) != 0;
403 p_status->b_has_carrier = (p_frontend->i_last_status & FE_HAS_CARRIER) != 0;
404 p_status->b_has_lock = (p_frontend->i_last_status & FE_HAS_LOCK) != 0;
406 static int ScanParametersDvbC( access_t *p_access, scan_parameter_t *p_scan )
408 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
411 memset( p_scan, 0, sizeof(*p_scan) );
412 p_scan->type = SCAN_DVB_C;
413 p_scan->b_exhaustive = false;
416 p_scan->frequency.i_min = p_frontend->info.frequency_min;
417 p_scan->frequency.i_max = p_frontend->info.frequency_max;
418 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
419 ? p_frontend->info.frequency_stepsize : 166667;
420 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
423 p_scan->bandwidth.i_min = 6;
424 p_scan->bandwidth.i_max = 8;
425 p_scan->bandwidth.i_step = 1;
426 p_scan->bandwidth.i_count = 3;
429 static int ScanParametersDvbT( access_t *p_access, scan_parameter_t *p_scan )
431 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
434 memset( p_scan, 0, sizeof(*p_scan) );
435 p_scan->type = SCAN_DVB_T;
436 p_scan->b_exhaustive = false;
439 p_scan->frequency.i_min = p_frontend->info.frequency_min;
440 p_scan->frequency.i_max = p_frontend->info.frequency_max;
441 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
442 ? p_frontend->info.frequency_stepsize : 166667;
443 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
446 p_scan->bandwidth.i_min = 6;
447 p_scan->bandwidth.i_max = 8;
448 p_scan->bandwidth.i_step = 1;
449 p_scan->bandwidth.i_count = 3;
452 int FrontendGetScanParameter( access_t *p_access, scan_parameter_t *p_scan )
454 access_sys_t *p_sys = p_access->p_sys;
455 const frontend_t *p_frontend = p_sys->p_frontend;
457 if( p_frontend->info.type == FE_OFDM ) // DVB-T
458 return ScanParametersDvbT( p_access, p_scan );
459 else if( p_frontend->info.type == FE_QAM ) // DVB-C
460 return ScanParametersDvbC( p_access, p_scan );
462 msg_Err( p_access, "Frontend type not supported for scanning" );
467 /*****************************************************************************
468 * FrontendStatus : Read frontend status
469 *****************************************************************************/
470 void FrontendStatus( access_t *p_access )
472 access_sys_t *p_sys = p_access->p_sys;
473 frontend_t *p_frontend = p_sys->p_frontend;
474 char *p = p_sys->psz_frontend_info = malloc( 10000 );
475 fe_status_t i_status;
478 /* Determine type of frontend */
479 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
480 &p_frontend->info )) < 0 )
483 strerror_r( errno, buf, sizeof( buf ) );
484 p += sprintf( p, "ioctl FE_GET_INFO failed (%d) %s\n", i_ret, buf );
488 /* Print out frontend capabilities. */
489 p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
490 p_frontend->info.name );
491 switch( p_frontend->info.type )
494 p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
497 p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
500 p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
502 #if 0 /* DVB_API_VERSION == 3 */
504 p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
507 p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
511 p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
512 p_frontend->info.type );
515 #define CHECK_INFO( x ) \
517 "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n", \
518 p_frontend->info.x );
520 CHECK_INFO( frequency_min );
521 CHECK_INFO( frequency_max );
522 CHECK_INFO( frequency_stepsize );
523 CHECK_INFO( frequency_tolerance );
524 CHECK_INFO( symbol_rate_min );
525 CHECK_INFO( symbol_rate_max );
526 CHECK_INFO( symbol_rate_tolerance );
527 CHECK_INFO( notifier_delay );
530 p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
532 #define CHECK_CAPS( x ) \
533 if ( p_frontend->info.caps & (FE_##x) ) \
534 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
536 CHECK_CAPS( IS_STUPID );
537 CHECK_CAPS( CAN_INVERSION_AUTO );
538 CHECK_CAPS( CAN_FEC_1_2 );
539 CHECK_CAPS( CAN_FEC_2_3 );
540 CHECK_CAPS( CAN_FEC_3_4 );
541 CHECK_CAPS( CAN_FEC_4_5 );
542 CHECK_CAPS( CAN_FEC_5_6 );
543 CHECK_CAPS( CAN_FEC_6_7 );
544 CHECK_CAPS( CAN_FEC_7_8 );
545 CHECK_CAPS( CAN_FEC_8_9 );
546 CHECK_CAPS( CAN_FEC_AUTO );
547 CHECK_CAPS( CAN_QPSK );
548 CHECK_CAPS( CAN_QAM_16 );
549 CHECK_CAPS( CAN_QAM_32 );
550 CHECK_CAPS( CAN_QAM_64 );
551 CHECK_CAPS( CAN_QAM_128 );
552 CHECK_CAPS( CAN_QAM_256 );
553 CHECK_CAPS( CAN_QAM_AUTO );
554 CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
555 CHECK_CAPS( CAN_BANDWIDTH_AUTO );
556 CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
557 CHECK_CAPS( CAN_HIERARCHY_AUTO );
558 CHECK_CAPS( CAN_MUTE_TS );
559 CHECK_CAPS( CAN_RECOVER );
560 #if 0 /* Disabled because of older distributions */
561 CHECK_CAPS( CAN_CLEAN_SETUP );
565 p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
567 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ))
571 strerror_r( errno, buf, sizeof( buf ) );
572 p += sprintf( p, "</table>ioctl FE_READ_STATUS failed (%d) %s\n",
577 #define CHECK_STATUS( x ) \
578 if ( i_status & (FE_##x) ) \
579 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
581 CHECK_STATUS( HAS_SIGNAL );
582 CHECK_STATUS( HAS_CARRIER );
583 CHECK_STATUS( HAS_VITERBI );
584 CHECK_STATUS( HAS_SYNC );
585 CHECK_STATUS( HAS_LOCK );
586 CHECK_STATUS( REINIT );
588 p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
591 if ( i_status & FE_HAS_LOCK )
594 p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
595 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
596 p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
598 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
600 p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
602 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
603 p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
606 p += sprintf( p, "</table>" );
609 vlc_mutex_lock( &p_sys->httpd_mutex );
610 p_sys->b_request_frontend_info = false;
611 vlc_cond_signal( &p_sys->httpd_cond );
612 vlc_mutex_unlock( &p_sys->httpd_mutex );
616 /*****************************************************************************
617 * FrontendInfo : Return information about given frontend
618 *****************************************************************************/
619 static int FrontendInfo( access_t *p_access )
621 access_sys_t *p_sys = p_access->p_sys;
622 frontend_t *p_frontend = p_sys->p_frontend;
625 /* Determine type of frontend */
626 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
627 &p_frontend->info )) < 0 )
629 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d): %m", i_ret );
633 /* Print out frontend capabilities. */
634 msg_Dbg(p_access, "Frontend Info:" );
635 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
636 switch( p_frontend->info.type )
639 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
642 msg_Dbg( p_access, " type = QAM (DVB-C)" );
645 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
648 msg_Dbg( p_access, " type = ATSC (USA)" );
650 #if 0 /* DVB_API_VERSION == 3 */
652 msg_Dbg(p_access, " type = MEMORY" );
655 msg_Dbg(p_access, " type = NETWORK" );
659 msg_Err( p_access, " unknown frontend type (%d)",
660 p_frontend->info.type );
663 msg_Dbg(p_access, " frequency_min = %u (kHz)",
664 p_frontend->info.frequency_min);
665 msg_Dbg(p_access, " frequency_max = %u (kHz)",
666 p_frontend->info.frequency_max);
667 msg_Dbg(p_access, " frequency_stepsize = %u",
668 p_frontend->info.frequency_stepsize);
669 msg_Dbg(p_access, " frequency_tolerance = %u",
670 p_frontend->info.frequency_tolerance);
671 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
672 p_frontend->info.symbol_rate_min);
673 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
674 p_frontend->info.symbol_rate_max);
675 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
676 p_frontend->info.symbol_rate_tolerance);
677 msg_Dbg(p_access, " notifier_delay (ms) = %u",
678 p_frontend->info.notifier_delay );
680 msg_Dbg(p_access, "Frontend Info capability list:");
681 if( p_frontend->info.caps & FE_IS_STUPID)
682 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
683 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
684 msg_Dbg(p_access, " inversion auto");
685 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
686 msg_Dbg(p_access, " forward error correction 1/2");
687 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
688 msg_Dbg(p_access, " forward error correction 2/3");
689 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
690 msg_Dbg(p_access, " forward error correction 3/4");
691 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
692 msg_Dbg(p_access, " forward error correction 4/5");
693 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
694 msg_Dbg(p_access, " forward error correction 5/6");
695 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
696 msg_Dbg(p_access, " forward error correction 6/7");
697 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
698 msg_Dbg(p_access, " forward error correction 7/8");
699 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
700 msg_Dbg(p_access, " forward error correction 8/9");
701 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
702 msg_Dbg(p_access, " forward error correction auto");
703 if( p_frontend->info.caps & FE_CAN_QPSK)
704 msg_Dbg(p_access, " card can do QPSK");
705 if( p_frontend->info.caps & FE_CAN_QAM_16)
706 msg_Dbg(p_access, " card can do QAM 16");
707 if( p_frontend->info.caps & FE_CAN_QAM_32)
708 msg_Dbg(p_access, " card can do QAM 32");
709 if( p_frontend->info.caps & FE_CAN_QAM_64)
710 msg_Dbg(p_access, " card can do QAM 64");
711 if( p_frontend->info.caps & FE_CAN_QAM_128)
712 msg_Dbg(p_access, " card can do QAM 128");
713 if( p_frontend->info.caps & FE_CAN_QAM_256)
714 msg_Dbg(p_access, " card can do QAM 256");
715 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
716 msg_Dbg(p_access, " card can do QAM auto");
717 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
718 msg_Dbg(p_access, " transmission mode auto");
719 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
720 msg_Dbg(p_access, " bandwidth mode auto");
721 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
722 msg_Dbg(p_access, " guard interval mode auto");
723 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
724 msg_Dbg(p_access, " hierarchy mode auto");
725 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
726 msg_Dbg(p_access, " card can mute TS");
727 if( p_frontend->info.caps & FE_CAN_RECOVER)
728 msg_Dbg(p_access, " card can recover from a cable unplug");
729 if( p_frontend->info.caps & FE_CAN_8VSB)
730 msg_Dbg(p_access, " card can do 8vsb");
731 if( p_frontend->info.caps & FE_CAN_16VSB)
732 msg_Dbg(p_access, " card can do 16vsb");
733 msg_Dbg(p_access, "End of capability list");
738 /*****************************************************************************
739 * Decoding the DVB parameters (common)
740 *****************************************************************************/
741 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
744 fe_spectral_inversion_t fe_inversion = 0;
746 i_val = var_GetInteger( p_access, "dvb-inversion" );
747 msg_Dbg( p_access, "using inversion=%d", i_val );
751 case 0: fe_inversion = INVERSION_OFF; break;
752 case 1: fe_inversion = INVERSION_ON; break;
753 case 2: fe_inversion = INVERSION_AUTO; break;
755 msg_Dbg( p_access, "dvb has inversion not set, using auto");
756 fe_inversion = INVERSION_AUTO;
762 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
764 fe_code_rate_t fe_fec = FEC_NONE;
766 msg_Dbg( p_access, "using fec=%d", i_val );
770 case 0: fe_fec = FEC_NONE; break;
771 case 1: fe_fec = FEC_1_2; break;
772 case 2: fe_fec = FEC_2_3; break;
773 case 3: fe_fec = FEC_3_4; break;
774 case 4: fe_fec = FEC_4_5; break;
775 case 5: fe_fec = FEC_5_6; break;
776 case 6: fe_fec = FEC_6_7; break;
777 case 7: fe_fec = FEC_7_8; break;
778 case 8: fe_fec = FEC_8_9; break;
779 case 9: fe_fec = FEC_AUTO; break;
783 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
789 static fe_modulation_t DecodeModulationQAM( access_t *p_access )
791 switch( var_GetInteger( p_access, "dvb-modulation" ) )
793 case 0: return QAM_AUTO;
794 case 16: return QAM_16;
795 case 32: return QAM_32;
796 case 64: return QAM_64;
797 case 128: return QAM_128;
798 case 256: return QAM_256;
800 msg_Dbg( p_access, "QAM modulation not set, using auto");
804 static fe_modulation_t DecodeModulationOFDM( access_t *p_access )
806 switch( var_GetInteger( p_access, "dvb-modulation" ) )
808 case -1: return QPSK;
809 case 0: return QAM_AUTO;
810 case 16: return QAM_16;
811 case 32: return QAM_32;
812 case 64: return QAM_64;
813 case 128: return QAM_128;
814 case 256: return QAM_256;
816 msg_Dbg( p_access, "OFDM modulation not set, using QAM auto");
820 static fe_modulation_t DecodeModulationATSC( access_t *p_access )
822 switch( var_GetInteger( p_access, "dvb-modulation" ) )
824 case 0: return QAM_AUTO;
825 case 8: return VSB_8;
826 case 16: return VSB_16;
827 case 32: return QAM_32;
828 case 64: return QAM_64;
829 case 128: return QAM_128;
830 case 256: return QAM_256;
832 msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
837 /*****************************************************************************
838 * FrontendSetQPSK : controls the FE device
839 *****************************************************************************/
840 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
843 fe_sec_voltage_t fe_voltage;
845 i_val = var_GetInteger( p_access, "dvb-voltage" );
846 msg_Dbg( p_access, "using voltage=%d", i_val );
850 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
851 case 13: fe_voltage = SEC_VOLTAGE_13; break;
852 case 18: fe_voltage = SEC_VOLTAGE_18; break;
854 fe_voltage = SEC_VOLTAGE_OFF;
855 msg_Err( p_access, "argument has invalid voltage (%d)", i_val );
861 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
864 fe_sec_tone_mode_t fe_tone;
866 i_val = var_GetInteger( p_access, "dvb-tone" );
867 msg_Dbg( p_access, "using tone=%d", i_val );
871 case 0: fe_tone = SEC_TONE_OFF; break;
872 case 1: fe_tone = SEC_TONE_ON; break;
874 fe_tone = SEC_TONE_OFF;
875 msg_Err( p_access, "argument has invalid tone mode (%d)", i_val );
883 struct dvb_diseqc_master_cmd cmd;
887 static int DoDiseqc( access_t *p_access )
889 access_sys_t *p_sys = p_access->p_sys;
892 int i_frequency, i_lnb_slof;
893 fe_sec_voltage_t fe_voltage;
894 fe_sec_tone_mode_t fe_tone;
897 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
898 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
900 i_val = var_GetInteger( p_access, "dvb-tone" );
901 if( i_val == -1 /* auto */ )
903 if( i_frequency >= i_lnb_slof )
907 var_SetInteger( p_access, "dvb-tone", i_val );
910 fe_voltage = DecodeVoltage( p_access );
911 fe_tone = DecodeTone( p_access );
913 /* Switch off continuous tone. */
914 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
916 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
917 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
921 /* Configure LNB voltage. */
922 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
924 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
929 b_val = var_GetBool( p_access, "dvb-high-voltage" );
930 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
931 b_val )) < 0 && b_val )
934 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
938 /* Wait for at least 15 ms. */
941 i_val = var_GetInteger( p_access, "dvb-satno" );
942 if( i_val > 0 && i_val < 5 )
944 /* digital satellite equipment control,
945 * specification is available from http://www.eutelsat.com/
948 /* 1.x compatible equipment */
949 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
951 /* param: high nibble: reset bits, low nibble set bits,
952 * bits are: option, position, polarization, band
954 cmd.cmd.msg[3] = 0xf0 /* reset bits */
955 | (((i_val - 1) * 4) & 0xc)
956 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
957 | (fe_tone == SEC_TONE_ON ? 1 : 0);
959 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
962 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
967 msleep(15000 + cmd.wait * 1000);
969 /* A or B simple diseqc ("diseqc-compatible") */
970 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
971 ((i_val - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
973 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
981 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
983 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
984 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
992 static int FrontendSetQPSK( access_t *p_access )
994 access_sys_t *p_sys = p_access->p_sys;
995 struct dvb_frontend_parameters fep;
998 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
1000 /* Prepare the fep structure */
1001 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
1003 i_val = var_GetInteger( p_access, "dvb-lnb-lof1" );
1006 /* Automatic mode. */
1007 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
1009 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
1012 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
1014 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
1015 i_lnb_lof1 = 3650000;
1017 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
1019 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
1021 i_lnb_lof1 = 5150000;
1023 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
1025 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
1027 i_lnb_lof1 = 5950000;
1029 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1031 msg_Dbg( p_access, "frequency %d is in Ku-band",
1033 i_lnb_lof1 = 9750000;
1034 i_lnb_lof2 = 10600000;
1035 i_lnb_slof = 11700000;
1039 msg_Err( p_access, "frequency %d is out of any known band",
1041 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1042 "oscillator frequency" );
1043 return VLC_EGENERIC;
1045 var_SetInteger( p_access, "dvb-lnb-lof1", i_lnb_lof1 );
1046 var_SetInteger( p_access, "dvb-lnb-lof2", i_lnb_lof2 );
1047 var_SetInteger( p_access, "dvb-lnb-slof", i_lnb_slof );
1052 i_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
1053 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
1056 if( i_lnb_slof && i_frequency >= i_lnb_slof )
1058 i_frequency -= i_lnb_lof2;
1062 i_frequency -= i_lnb_lof1;
1064 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1066 fep.inversion = DecodeInversion( p_access );
1068 fep.u.qpsk.symbol_rate = var_GetInteger( p_access, "dvb-srate" );
1070 fep.u.qpsk.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access, "dvb-fec" ) );
1072 if( DoDiseqc( p_access ) < 0 )
1074 return VLC_EGENERIC;
1077 /* Empty the event queue */
1080 struct dvb_frontend_event event;
1081 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1082 && errno == EWOULDBLOCK )
1086 /* Now send it all to the frontend device */
1087 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1089 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1090 return VLC_EGENERIC;
1096 /*****************************************************************************
1097 * FrontendSetQAM : controls the FE device
1098 *****************************************************************************/
1099 static int FrontendSetQAM( access_t *p_access )
1101 access_sys_t *p_sys = p_access->p_sys;
1102 frontend_t *p_frontend = p_sys->p_frontend;
1103 struct dvb_frontend_parameters fep;
1107 /* Prepare the fep structure */
1109 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1111 fep.inversion = DecodeInversion( p_access );
1113 /* Default symbol-rate is for dvb-s, and doesn't fit
1114 * for dvb-c, so if it's over the limit of frontend, default to
1115 * somewhat common value
1117 i_val = var_GetInteger( p_access, "dvb-srate" );
1118 if( i_val < p_frontend->info.symbol_rate_max &&
1119 i_val > p_frontend->info.symbol_rate_min )
1120 fep.u.qam.symbol_rate = i_val;
1122 fep.u.qam.symbol_rate = 6875000;
1124 fep.u.qam.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access,
1127 fep.u.qam.modulation = DecodeModulationQAM( p_access );
1129 /* Empty the event queue */
1132 struct dvb_frontend_event event;
1133 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1134 && errno == EWOULDBLOCK )
1138 /* Now send it all to the frontend device */
1139 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1141 msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1142 return VLC_EGENERIC;
1148 /*****************************************************************************
1149 * FrontendSetOFDM : controls the FE device
1150 *****************************************************************************/
1151 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1153 fe_bandwidth_t fe_bandwidth = 0;
1154 int i_bandwith = var_GetInteger( p_access, "dvb-bandwidth" );
1156 msg_Dbg( p_access, "using bandwidth=%d", i_bandwith );
1158 switch( i_bandwith )
1160 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1161 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1162 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1163 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1165 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1166 fe_bandwidth = BANDWIDTH_AUTO;
1169 return fe_bandwidth;
1172 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1174 fe_transmit_mode_t fe_transmission = 0;
1175 int i_transmission = var_GetInteger( p_access, "dvb-transmission" );
1177 msg_Dbg( p_access, "using transmission=%d", i_transmission );
1179 switch( i_transmission )
1181 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1182 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1183 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1185 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1186 fe_transmission = TRANSMISSION_MODE_AUTO;
1189 return fe_transmission;
1192 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1194 fe_guard_interval_t fe_guard = 0;
1195 int i_guard = var_GetInteger( p_access, "dvb-guard" );
1197 msg_Dbg( p_access, "using guard=%d", i_guard );
1201 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1202 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1203 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1204 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1205 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1207 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1208 fe_guard = GUARD_INTERVAL_AUTO;
1214 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1216 fe_hierarchy_t fe_hierarchy = 0;
1217 int i_hierarchy = var_GetInteger( p_access, "dvb-hierarchy" );
1219 msg_Dbg( p_access, "using hierarchy=%d", i_hierarchy );
1221 switch( i_hierarchy )
1223 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1224 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1225 case 1: fe_hierarchy = HIERARCHY_1; break;
1226 case 2: fe_hierarchy = HIERARCHY_2; break;
1227 case 4: fe_hierarchy = HIERARCHY_4; break;
1229 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1230 fe_hierarchy = HIERARCHY_AUTO;
1233 return fe_hierarchy;
1236 static int FrontendSetOFDM( access_t * p_access )
1238 access_sys_t *p_sys = p_access->p_sys;
1239 struct dvb_frontend_parameters fep;
1242 /* Prepare the fep structure */
1244 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1246 fep.inversion = DecodeInversion( p_access );
1248 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1249 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, var_GetInteger( p_access,
1250 "dvb-code-rate-hp" ) );
1251 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, var_GetInteger( p_access,
1252 "dvb-code-rate-lp" ) );
1253 fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1254 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1255 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1256 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1258 /* Empty the event queue */
1261 struct dvb_frontend_event event;
1262 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1263 && errno == EWOULDBLOCK )
1267 /* Now send it all to the frontend device */
1268 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1270 msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1277 /*****************************************************************************
1278 * FrontendSetATSC : controls the FE device
1279 *****************************************************************************/
1280 static int FrontendSetATSC( access_t *p_access )
1282 access_sys_t *p_sys = p_access->p_sys;
1283 struct dvb_frontend_parameters fep;
1286 /* Prepare the fep structure */
1288 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1290 fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1292 /* Empty the event queue */
1295 struct dvb_frontend_event event;
1296 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1297 && errno == EWOULDBLOCK )
1301 /* Now send it all to the frontend device */
1302 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1304 msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1305 return VLC_EGENERIC;
1316 /*****************************************************************************
1317 * DMXSetFilter : controls the demux to add a filter
1318 *****************************************************************************/
1319 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1321 struct dmx_pes_filter_params s_filter_params;
1323 unsigned int i_adapter, i_device;
1326 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1327 i_device = var_GetInteger( p_access, "dvb-device" );
1329 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1330 >= (int)sizeof(dmx) )
1332 msg_Err( p_access, "snprintf() truncated string for DMX" );
1333 dmx[sizeof(dmx) - 1] = '\0';
1336 msg_Dbg( p_access, "Opening device %s", dmx );
1337 if( (*pi_fd = utf8_open(dmx, O_RDWR)) < 0 )
1339 msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1340 return VLC_EGENERIC;
1343 /* We fill the DEMUX structure : */
1344 s_filter_params.pid = i_pid;
1345 s_filter_params.input = DMX_IN_FRONTEND;
1346 s_filter_params.output = DMX_OUT_TS_TAP;
1347 s_filter_params.flags = DMX_IMMEDIATE_START;
1350 { /* First device */
1352 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1353 s_filter_params.pes_type = DMX_PES_VIDEO0;
1356 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1357 s_filter_params.pes_type = DMX_PES_AUDIO0;
1360 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1361 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1364 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1365 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1368 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1369 s_filter_params.pes_type = DMX_PES_PCR0;
1373 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1374 s_filter_params.pes_type = DMX_PES_VIDEO1;
1377 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1378 s_filter_params.pes_type = DMX_PES_AUDIO1;
1381 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1382 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1385 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1386 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1389 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1390 s_filter_params.pes_type = DMX_PES_PCR1;
1394 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1395 s_filter_params.pes_type = DMX_PES_VIDEO2;
1398 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1399 s_filter_params.pes_type = DMX_PES_AUDIO2;
1402 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1403 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1406 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1407 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1410 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1411 s_filter_params.pes_type = DMX_PES_PCR2;
1415 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1416 s_filter_params.pes_type = DMX_PES_VIDEO3;
1419 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1420 s_filter_params.pes_type = DMX_PES_AUDIO3;
1423 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1424 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1427 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1428 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1431 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1432 s_filter_params.pes_type = DMX_PES_PCR3;
1434 /* Usually used by Nova cards */
1437 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1438 s_filter_params.pes_type = DMX_PES_OTHER;
1442 /* We then give the order to the device : */
1443 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1445 msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1446 return VLC_EGENERIC;
1451 /*****************************************************************************
1452 * DMXUnsetFilter : removes a filter
1453 *****************************************************************************/
1454 int DMXUnsetFilter( access_t * p_access, int i_fd )
1458 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1460 msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1464 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1474 /*****************************************************************************
1476 *****************************************************************************/
1477 int DVROpen( access_t * p_access )
1479 access_sys_t *p_sys = p_access->p_sys;
1480 unsigned int i_adapter, i_device;
1483 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1484 i_device = var_GetInteger( p_access, "dvb-device" );
1486 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1487 >= (int)sizeof(dvr) )
1489 msg_Err( p_access, "snprintf() truncated string for DVR" );
1490 dvr[sizeof(dvr) - 1] = '\0';
1493 msg_Dbg( p_access, "Opening device %s", dvr );
1494 if( (p_sys->i_handle = utf8_open(dvr, O_RDONLY)) < 0 )
1496 msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1497 return VLC_EGENERIC;
1500 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1502 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1508 /*****************************************************************************
1510 *****************************************************************************/
1511 void DVRClose( access_t * p_access )
1513 access_sys_t *p_sys = p_access->p_sys;
1515 close( p_sys->i_handle );
1523 /*****************************************************************************
1525 *****************************************************************************/
1526 int CAMOpen( access_t *p_access )
1528 access_sys_t *p_sys = p_access->p_sys;
1530 int i_adapter, i_device;
1533 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1534 i_device = var_GetInteger( p_access, "dvb-device" );
1536 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1538 msg_Err( p_access, "snprintf() truncated string for CA" );
1539 ca[sizeof(ca) - 1] = '\0';
1541 memset( &caps, 0, sizeof( ca_caps_t ));
1543 msg_Dbg( p_access, "Opening device %s", ca );
1544 if( (p_sys->i_ca_handle = utf8_open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1546 msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1547 p_sys->i_ca_handle = 0;
1548 return VLC_EGENERIC;
1551 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1553 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1554 close( p_sys->i_ca_handle );
1555 p_sys->i_ca_handle = 0;
1556 return VLC_EGENERIC;
1559 /* Output CA capabilities */
1560 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1561 caps.slot_num == 1 ? "slot" : "slots" );
1562 if ( caps.slot_type & CA_CI )
1563 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1564 if ( caps.slot_type & CA_CI_LINK )
1565 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1566 if ( caps.slot_type & CA_CI_PHYS )
1567 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1568 if ( caps.slot_type & CA_DESCR )
1569 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1570 if ( caps.slot_type & CA_SC )
1571 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1573 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1574 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1575 if ( caps.descr_type & CA_ECD )
1576 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1577 if ( caps.descr_type & CA_NDS )
1578 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1579 if ( caps.descr_type & CA_DSS )
1580 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1582 if ( caps.slot_num == 0 )
1584 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1585 close( p_sys->i_ca_handle );
1586 p_sys->i_ca_handle = 0;
1587 return VLC_EGENERIC;
1590 if( caps.slot_type & CA_CI_LINK )
1592 p_sys->i_ca_type = CA_CI_LINK;
1594 else if( caps.slot_type & CA_CI )
1596 p_sys->i_ca_type = CA_CI;
1600 p_sys->i_ca_type = -1;
1601 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1602 close( p_sys->i_ca_handle );
1603 p_sys->i_ca_handle = 0;
1604 return VLC_EGENERIC;
1607 p_sys->i_nb_slots = caps.slot_num;
1608 memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1609 memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1610 memset( p_sys->pb_slot_mmi_undisplayed, 0,
1611 sizeof(bool) * MAX_CI_SLOTS );
1613 return en50221_Init( p_access );
1616 /*****************************************************************************
1618 *****************************************************************************/
1619 int CAMPoll( access_t * p_access )
1621 access_sys_t *p_sys = p_access->p_sys;
1622 int i_ret = VLC_EGENERIC;
1624 if ( p_sys->i_ca_handle == 0 )
1626 return VLC_EGENERIC;
1629 switch( p_sys->i_ca_type )
1632 i_ret = en50221_Poll( p_access );
1635 i_ret = VLC_SUCCESS;
1638 msg_Err( p_access, "CAMPoll: This should not happen" );
1646 /*****************************************************************************
1648 *****************************************************************************/
1649 void CAMStatus( access_t * p_access )
1651 access_sys_t *p_sys = p_access->p_sys;
1656 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1658 /* Check if we have an undisplayed MMI message : in that case we ignore
1659 * the user input to avoid confusing the CAM. */
1660 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1662 if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1664 p_sys->psz_request = NULL;
1666 "ignoring user request because of a new MMI object" );
1672 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1674 /* We have a mission to accomplish. */
1675 en50221_mmi_object_t mmi_object;
1676 char *psz_request = p_sys->psz_request;
1677 char psz_value[255];
1681 p_sys->psz_request = NULL;
1683 if ( HTTPExtractValue( psz_request, "slot", psz_value,
1684 sizeof(psz_value) ) == NULL )
1686 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1689 i_slot = atoi(psz_value);
1691 if ( HTTPExtractValue( psz_request, "open", psz_value,
1692 sizeof(psz_value) ) != NULL )
1694 en50221_OpenMMI( p_access, i_slot );
1698 if ( HTTPExtractValue( psz_request, "close", psz_value,
1699 sizeof(psz_value) ) != NULL )
1701 en50221_CloseMMI( p_access, i_slot );
1705 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1706 sizeof(psz_value) ) == NULL )
1711 if ( HTTPExtractValue( psz_request, "type", psz_value,
1712 sizeof(psz_value) ) == NULL )
1714 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1718 if ( !strcmp( psz_value, "enq" ) )
1720 mmi_object.i_object_type = EN50221_MMI_ANSW;
1721 mmi_object.u.answ.b_ok = b_ok;
1722 if ( b_ok == false )
1724 mmi_object.u.answ.psz_answ = strdup("");
1728 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1729 sizeof(psz_value) ) == NULL )
1731 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1735 mmi_object.u.answ.psz_answ = strdup(psz_value);
1740 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1741 if ( b_ok == false )
1743 mmi_object.u.menu_answ.i_choice = 0;
1747 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1748 sizeof(psz_value) ) == NULL )
1749 mmi_object.u.menu_answ.i_choice = 0;
1751 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1755 en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1759 /* Check that we have all necessary MMI information. */
1760 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1762 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1766 p = p_sys->psz_mmi_info = malloc( 10000 );
1768 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1771 strerror_r( errno, buf, sizeof( buf ) );
1772 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1776 /* Output CA capabilities */
1777 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1778 caps.slot_num == 1 ? "slot" : "slots" );
1779 #define CHECK_CAPS( x, s ) \
1780 if ( caps.slot_type & (CA_##x) ) \
1781 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1783 CHECK_CAPS( CI, "CI high level interface" );
1784 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1785 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1786 CHECK_CAPS( DESCR, "built-in descrambler" );
1787 CHECK_CAPS( SC, "simple smartcard interface" );
1790 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1791 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1792 #define CHECK_DESC( x ) \
1793 if ( caps.descr_type & (CA_##x) ) \
1794 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1801 p += sprintf( p, "</table>" );
1803 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1805 ca_slot_info_t sinfo;
1807 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1808 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1811 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1814 strerror_r( errno, buf, sizeof( buf ) );
1815 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1819 #define CHECK_TYPE( x, s ) \
1820 if ( sinfo.type & (CA_##x) ) \
1821 p += sprintf( p, "%s", s );
1823 CHECK_TYPE( CI, "high level, " );
1824 CHECK_TYPE( CI_LINK, "link layer level, " );
1825 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1828 if ( sinfo.flags & CA_CI_MODULE_READY )
1830 en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1833 p += sprintf( p, "module present and ready<p>\n" );
1834 p += sprintf( p, "<form action=index.html method=get>\n" );
1835 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1838 if ( p_object == NULL )
1840 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1844 switch ( p_object->i_object_type )
1846 case EN50221_MMI_ENQ:
1847 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1848 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1849 p_object->u.enq.psz_text );
1850 if ( p_object->u.enq.b_blind == false )
1851 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1853 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1856 case EN50221_MMI_MENU:
1857 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1858 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1859 p_object->u.menu.psz_title );
1860 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1861 p_object->u.menu.psz_subtitle );
1862 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1863 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1864 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1865 p_object->u.menu.psz_bottom );
1868 case EN50221_MMI_LIST:
1869 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1870 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1871 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1872 p_object->u.menu.psz_title );
1873 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1874 p_object->u.menu.psz_subtitle );
1875 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1876 p += sprintf( p, "%s<br>\n",
1877 p_object->u.menu.ppsz_choices[i] );
1878 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1879 p_object->u.menu.psz_bottom );
1883 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1886 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1887 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1888 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1890 p += sprintf( p, "</form>\n" );
1892 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1893 p += sprintf( p, "module present, not ready<br>\n" );
1895 p += sprintf( p, "module not present<br>\n" );
1899 vlc_mutex_lock( &p_sys->httpd_mutex );
1900 p_sys->b_request_mmi_info = false;
1901 vlc_cond_signal( &p_sys->httpd_cond );
1902 vlc_mutex_unlock( &p_sys->httpd_mutex );
1906 /*****************************************************************************
1908 *****************************************************************************/
1909 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1911 access_sys_t *p_sys = p_access->p_sys;
1913 if( p_sys->i_ca_handle == 0 )
1915 dvbpsi_DeletePMT( p_pmt );
1916 return VLC_EGENERIC;
1919 en50221_SetCAPMT( p_access, p_pmt );
1924 /*****************************************************************************
1926 *****************************************************************************/
1927 void CAMClose( access_t * p_access )
1929 access_sys_t *p_sys = p_access->p_sys;
1931 en50221_End( p_access );
1933 if ( p_sys->i_ca_handle )
1935 close( p_sys->i_ca_handle );