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>
44 /* DVB Card Drivers */
45 #include <linux/dvb/version.h>
46 #include <linux/dvb/dmx.h>
47 #include <linux/dvb/frontend.h>
48 #include <linux/dvb/ca.h>
50 /* Include dvbpsi headers */
51 #ifdef HAVE_DVBPSI_DR_H
52 # include <dvbpsi/dvbpsi.h>
53 # include <dvbpsi/descriptor.h>
54 # include <dvbpsi/pat.h>
55 # include <dvbpsi/pmt.h>
56 # include <dvbpsi/dr.h>
57 # include <dvbpsi/psi.h>
58 # include <dvbpsi/demux.h>
59 # include <dvbpsi/sdt.h>
62 # include "descriptor.h"
63 # include "tables/pat.h"
64 # include "tables/pmt.h"
65 # include "descriptors/dr.h"
68 # include "tables/sdt.h"
72 # include "vlc_httpd.h"
82 fe_status_t i_last_status;
83 struct dvb_frontend_info info;
86 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
88 /* Local prototypes */
89 static int FrontendInfo( access_t * );
90 static int FrontendSetQPSK( access_t * );
91 static int FrontendSetQAM( access_t * );
92 static int FrontendSetOFDM( access_t * );
93 static int FrontendSetATSC( access_t * );
95 /*****************************************************************************
96 * FrontendOpen : Determine frontend device information and capabilities
97 *****************************************************************************/
98 int FrontendOpen( access_t *p_access )
100 access_sys_t *p_sys = p_access->p_sys;
101 frontend_t * p_frontend;
102 unsigned int i_adapter, i_device;
106 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
107 i_device = var_GetInteger( p_access, "dvb-device" );
108 b_probe = var_GetBool( p_access, "dvb-probe" );
110 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
112 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
113 frontend[sizeof(frontend) - 1] = '\0';
116 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
120 msg_Dbg( p_access, "Opening device %s", frontend );
121 if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
123 msg_Err( p_access, "FrontEndOpen: opening device failed (%m)" );
130 const char * psz_expected = NULL;
131 const char * psz_real;
133 if( FrontendInfo( p_access ) < 0 )
135 close( p_sys->i_frontend_handle );
140 switch( p_frontend->info.type )
155 psz_real = "unknown";
159 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
160 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
161 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
162 (p_frontend->info.type != FE_QPSK) )
164 psz_expected = "DVB-S";
166 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
167 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
168 (p_frontend->info.type != FE_QAM) )
170 psz_expected = "DVB-C";
172 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
173 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
174 (p_frontend->info.type != FE_OFDM) )
176 psz_expected = "DVB-T";
179 if( (!strncmp( p_access->psz_access, "usdigital", 9 ) ||
180 !strncmp( p_access->psz_access, "atsc", 4 ) ) &&
181 (p_frontend->info.type != FE_ATSC) )
183 psz_expected = "ATSC";
186 if( psz_expected != NULL )
188 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
189 psz_expected, psz_real );
190 close( p_sys->i_frontend_handle );
195 else /* no frontend probing is done so use default border values. */
197 msg_Dbg( p_access, "using default values for frontend info" );
199 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
200 p_frontend->info.type = FE_QPSK;
201 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
202 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
203 p_frontend->info.type = FE_QPSK;
204 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
205 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
206 p_frontend->info.type = FE_QAM;
207 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
208 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
209 p_frontend->info.type = FE_OFDM;
210 else if( !strncmp( p_access->psz_access, "usdigital", 9 ) ||
211 !strncmp( p_access->psz_access, "atsc", 4 ) )
212 p_frontend->info.type = FE_ATSC;
218 /*****************************************************************************
219 * FrontendClose : Close the frontend
220 *****************************************************************************/
221 void FrontendClose( access_t *p_access )
223 access_sys_t *p_sys = p_access->p_sys;
225 if( p_sys->p_frontend )
227 close( p_sys->i_frontend_handle );
228 free( p_sys->p_frontend );
230 p_sys->p_frontend = NULL;
234 /*****************************************************************************
235 * FrontendSet : Tune !
236 *****************************************************************************/
237 int FrontendSet( access_t *p_access )
239 access_sys_t *p_sys = p_access->p_sys;
241 switch( p_sys->p_frontend->info.type )
245 if( FrontendSetQPSK( p_access ) < 0 )
247 msg_Err( p_access, "DVB-S: tuning failed" );
254 if( FrontendSetQAM( p_access ) < 0 )
256 msg_Err( p_access, "DVB-C: tuning failed" );
263 if( FrontendSetOFDM( p_access ) < 0 )
265 msg_Err( p_access, "DVB-T: tuning failed" );
272 if( FrontendSetATSC( p_access ) < 0 )
274 msg_Err( p_access, "ATSC: tuning failed" );
280 msg_Err( p_access, "Could not determine frontend type on %s",
281 p_sys->p_frontend->info.name );
284 p_sys->p_frontend->i_last_status = 0;
285 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
289 /*****************************************************************************
290 * FrontendPoll : Poll for frontend events
291 *****************************************************************************/
292 void FrontendPoll( access_t *p_access )
294 access_sys_t *p_sys = p_access->p_sys;
295 frontend_t * p_frontend = p_sys->p_frontend;
296 struct dvb_frontend_event event;
297 fe_status_t i_status, i_diff;
301 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
305 if( errno == EWOULDBLOCK )
306 return; /* no more events */
308 msg_Err( p_access, "reading frontend event failed (%d): %m",
313 i_status = event.status;
314 i_diff = i_status ^ p_frontend->i_last_status;
315 p_frontend->i_last_status = i_status;
320 if ( i_diff & (x) ) \
322 if ( i_status & (x) )
324 IF_UP( FE_HAS_SIGNAL )
325 msg_Dbg( p_access, "frontend has acquired signal" );
327 msg_Dbg( p_access, "frontend has lost signal" );
329 IF_UP( FE_HAS_CARRIER )
330 msg_Dbg( p_access, "frontend has acquired carrier" );
332 msg_Dbg( p_access, "frontend has lost carrier" );
334 IF_UP( FE_HAS_VITERBI )
335 msg_Dbg( p_access, "frontend has acquired stable FEC" );
337 msg_Dbg( p_access, "frontend has lost FEC" );
340 msg_Dbg( p_access, "frontend has acquired sync" );
342 msg_Dbg( p_access, "frontend has lost sync" );
346 frontend_statistic_t stat;
348 msg_Dbg( p_access, "frontend has acquired lock" );
349 p_sys->i_frontend_timeout = 0;
351 /* Read some statistics */
352 if( !FrontendGetStatistic( p_access, &stat ) )
354 if( stat.i_ber >= 0 )
355 msg_Dbg( p_access, "- Bit error rate: %d", stat.i_ber );
356 if( stat.i_signal_strenth >= 0 )
357 msg_Dbg( p_access, "- Signal strength: %d", stat.i_signal_strenth );
358 if( stat.i_snr >= 0 )
359 msg_Dbg( p_access, "- SNR: %d", stat.i_snr );
364 msg_Dbg( p_access, "frontend has lost lock" );
365 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
370 /* The frontend was reinited. */
371 msg_Warn( p_access, "reiniting frontend");
372 FrontendSet( p_access );
378 int FrontendGetStatistic( access_t *p_access, frontend_statistic_t *p_stat )
380 access_sys_t *p_sys = p_access->p_sys;
381 frontend_t * p_frontend = p_sys->p_frontend;
383 if( (p_frontend->i_last_status & FE_HAS_LOCK) == 0 )
386 memset( p_stat, 0, sizeof(*p_stat) );
387 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &p_stat->i_ber ) < 0 )
389 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &p_stat->i_signal_strenth ) < 0 )
390 p_stat->i_signal_strenth = -1;
391 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &p_stat->i_snr ) < 0 )
396 void FrontendGetStatus( access_t *p_access, frontend_status_t *p_status )
398 access_sys_t *p_sys = p_access->p_sys;
399 frontend_t * p_frontend = p_sys->p_frontend;
401 p_status->b_has_signal = (p_frontend->i_last_status & FE_HAS_SIGNAL) != 0;
402 p_status->b_has_carrier = (p_frontend->i_last_status & FE_HAS_CARRIER) != 0;
403 p_status->b_has_lock = (p_frontend->i_last_status & FE_HAS_LOCK) != 0;
405 static int ScanParametersDvbT( access_t *p_access, scan_parameter_t *p_scan )
407 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
410 memset( p_scan, 0, sizeof(*p_scan) );
411 p_scan->type = SCAN_DVB_T;
412 p_scan->b_exhaustive = false;
415 p_scan->frequency.i_min = p_frontend->info.frequency_min;
416 p_scan->frequency.i_max = p_frontend->info.frequency_max;
417 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize;
418 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
421 p_scan->bandwidth.i_min = 6;
422 p_scan->bandwidth.i_max = 8;
423 p_scan->bandwidth.i_step = 1;
424 p_scan->bandwidth.i_count = 3;
427 int FrontendGetScanParameter( access_t *p_access, scan_parameter_t *p_scan )
429 access_sys_t *p_sys = p_access->p_sys;
430 const frontend_t *p_frontend = p_sys->p_frontend;
432 if( p_frontend->info.type == FE_OFDM ) // DVB-T
433 return ScanParametersDvbT( p_access, p_scan );
435 msg_Err( p_access, "Frontend type not supported for scanning" );
440 /*****************************************************************************
441 * FrontendStatus : Read frontend status
442 *****************************************************************************/
443 void FrontendStatus( access_t *p_access )
445 access_sys_t *p_sys = p_access->p_sys;
446 frontend_t *p_frontend = p_sys->p_frontend;
447 char *p = p_sys->psz_frontend_info = malloc( 10000 );
448 fe_status_t i_status;
451 /* Determine type of frontend */
452 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
453 &p_frontend->info )) < 0 )
456 strerror_r( errno, buf, sizeof( buf ) );
457 p += sprintf( p, "ioctl FE_GET_INFO failed (%d) %s\n", i_ret, buf );
461 /* Print out frontend capabilities. */
462 p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
463 p_frontend->info.name );
464 switch( p_frontend->info.type )
467 p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
470 p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
473 p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
475 #if 0 /* DVB_API_VERSION == 3 */
477 p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
480 p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
484 p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
485 p_frontend->info.type );
488 #define CHECK_INFO( x ) \
490 "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n", \
491 p_frontend->info.x );
493 CHECK_INFO( frequency_min );
494 CHECK_INFO( frequency_max );
495 CHECK_INFO( frequency_stepsize );
496 CHECK_INFO( frequency_tolerance );
497 CHECK_INFO( symbol_rate_min );
498 CHECK_INFO( symbol_rate_max );
499 CHECK_INFO( symbol_rate_tolerance );
500 CHECK_INFO( notifier_delay );
503 p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
505 #define CHECK_CAPS( x ) \
506 if ( p_frontend->info.caps & (FE_##x) ) \
507 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
509 CHECK_CAPS( IS_STUPID );
510 CHECK_CAPS( CAN_INVERSION_AUTO );
511 CHECK_CAPS( CAN_FEC_1_2 );
512 CHECK_CAPS( CAN_FEC_2_3 );
513 CHECK_CAPS( CAN_FEC_3_4 );
514 CHECK_CAPS( CAN_FEC_4_5 );
515 CHECK_CAPS( CAN_FEC_5_6 );
516 CHECK_CAPS( CAN_FEC_6_7 );
517 CHECK_CAPS( CAN_FEC_7_8 );
518 CHECK_CAPS( CAN_FEC_8_9 );
519 CHECK_CAPS( CAN_FEC_AUTO );
520 CHECK_CAPS( CAN_QPSK );
521 CHECK_CAPS( CAN_QAM_16 );
522 CHECK_CAPS( CAN_QAM_32 );
523 CHECK_CAPS( CAN_QAM_64 );
524 CHECK_CAPS( CAN_QAM_128 );
525 CHECK_CAPS( CAN_QAM_256 );
526 CHECK_CAPS( CAN_QAM_AUTO );
527 CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
528 CHECK_CAPS( CAN_BANDWIDTH_AUTO );
529 CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
530 CHECK_CAPS( CAN_HIERARCHY_AUTO );
531 CHECK_CAPS( CAN_MUTE_TS );
532 CHECK_CAPS( CAN_RECOVER );
533 #if 0 /* Disabled because of older distributions */
534 CHECK_CAPS( CAN_CLEAN_SETUP );
538 p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
540 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ))
544 strerror_r( errno, buf, sizeof( buf ) );
545 p += sprintf( p, "</table>ioctl FE_READ_STATUS failed (%d) %s\n",
550 #define CHECK_STATUS( x ) \
551 if ( i_status & (FE_##x) ) \
552 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
554 CHECK_STATUS( HAS_SIGNAL );
555 CHECK_STATUS( HAS_CARRIER );
556 CHECK_STATUS( HAS_VITERBI );
557 CHECK_STATUS( HAS_SYNC );
558 CHECK_STATUS( HAS_LOCK );
559 CHECK_STATUS( REINIT );
561 p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
564 if ( i_status & FE_HAS_LOCK )
567 p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
568 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
569 p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
571 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
573 p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
575 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
576 p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
579 p += sprintf( p, "</table>" );
582 vlc_mutex_lock( &p_sys->httpd_mutex );
583 p_sys->b_request_frontend_info = false;
584 vlc_cond_signal( &p_sys->httpd_cond );
585 vlc_mutex_unlock( &p_sys->httpd_mutex );
589 /*****************************************************************************
590 * FrontendInfo : Return information about given frontend
591 *****************************************************************************/
592 static int FrontendInfo( access_t *p_access )
594 access_sys_t *p_sys = p_access->p_sys;
595 frontend_t *p_frontend = p_sys->p_frontend;
598 /* Determine type of frontend */
599 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
600 &p_frontend->info )) < 0 )
602 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d): %m", i_ret );
606 /* Print out frontend capabilities. */
607 msg_Dbg(p_access, "Frontend Info:" );
608 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
609 switch( p_frontend->info.type )
612 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
615 msg_Dbg( p_access, " type = QAM (DVB-C)" );
618 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
621 msg_Dbg( p_access, " type = ATSC (USA)" );
623 #if 0 /* DVB_API_VERSION == 3 */
625 msg_Dbg(p_access, " type = MEMORY" );
628 msg_Dbg(p_access, " type = NETWORK" );
632 msg_Err( p_access, " unknown frontend type (%d)",
633 p_frontend->info.type );
636 msg_Dbg(p_access, " frequency_min = %u (kHz)",
637 p_frontend->info.frequency_min);
638 msg_Dbg(p_access, " frequency_max = %u (kHz)",
639 p_frontend->info.frequency_max);
640 msg_Dbg(p_access, " frequency_stepsize = %u",
641 p_frontend->info.frequency_stepsize);
642 msg_Dbg(p_access, " frequency_tolerance = %u",
643 p_frontend->info.frequency_tolerance);
644 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
645 p_frontend->info.symbol_rate_min);
646 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
647 p_frontend->info.symbol_rate_max);
648 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
649 p_frontend->info.symbol_rate_tolerance);
650 msg_Dbg(p_access, " notifier_delay (ms) = %u",
651 p_frontend->info.notifier_delay );
653 msg_Dbg(p_access, "Frontend Info capability list:");
654 if( p_frontend->info.caps & FE_IS_STUPID)
655 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
656 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
657 msg_Dbg(p_access, " inversion auto");
658 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
659 msg_Dbg(p_access, " forward error correction 1/2");
660 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
661 msg_Dbg(p_access, " forward error correction 2/3");
662 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
663 msg_Dbg(p_access, " forward error correction 3/4");
664 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
665 msg_Dbg(p_access, " forward error correction 4/5");
666 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
667 msg_Dbg(p_access, " forward error correction 5/6");
668 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
669 msg_Dbg(p_access, " forward error correction 6/7");
670 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
671 msg_Dbg(p_access, " forward error correction 7/8");
672 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
673 msg_Dbg(p_access, " forward error correction 8/9");
674 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
675 msg_Dbg(p_access, " forward error correction auto");
676 if( p_frontend->info.caps & FE_CAN_QPSK)
677 msg_Dbg(p_access, " card can do QPSK");
678 if( p_frontend->info.caps & FE_CAN_QAM_16)
679 msg_Dbg(p_access, " card can do QAM 16");
680 if( p_frontend->info.caps & FE_CAN_QAM_32)
681 msg_Dbg(p_access, " card can do QAM 32");
682 if( p_frontend->info.caps & FE_CAN_QAM_64)
683 msg_Dbg(p_access, " card can do QAM 64");
684 if( p_frontend->info.caps & FE_CAN_QAM_128)
685 msg_Dbg(p_access, " card can do QAM 128");
686 if( p_frontend->info.caps & FE_CAN_QAM_256)
687 msg_Dbg(p_access, " card can do QAM 256");
688 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
689 msg_Dbg(p_access, " card can do QAM auto");
690 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
691 msg_Dbg(p_access, " transmission mode auto");
692 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
693 msg_Dbg(p_access, " bandwidth mode auto");
694 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
695 msg_Dbg(p_access, " guard interval mode auto");
696 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
697 msg_Dbg(p_access, " hierarchy mode auto");
698 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
699 msg_Dbg(p_access, " card can mute TS");
700 if( p_frontend->info.caps & FE_CAN_RECOVER)
701 msg_Dbg(p_access, " card can recover from a cable unplug");
702 if( p_frontend->info.caps & FE_CAN_8VSB)
703 msg_Dbg(p_access, " card can do 8vsb");
704 if( p_frontend->info.caps & FE_CAN_16VSB)
705 msg_Dbg(p_access, " card can do 16vsb");
706 msg_Dbg(p_access, "End of capability list");
711 /*****************************************************************************
712 * Decoding the DVB parameters (common)
713 *****************************************************************************/
714 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
717 fe_spectral_inversion_t fe_inversion = 0;
719 var_Get( p_access, "dvb-inversion", &val );
720 msg_Dbg( p_access, "using inversion=%d", val.i_int );
724 case 0: fe_inversion = INVERSION_OFF; break;
725 case 1: fe_inversion = INVERSION_ON; break;
726 case 2: fe_inversion = INVERSION_AUTO; break;
728 msg_Dbg( p_access, "dvb has inversion not set, using auto");
729 fe_inversion = INVERSION_AUTO;
735 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
737 fe_code_rate_t fe_fec = FEC_NONE;
739 msg_Dbg( p_access, "using fec=%d", i_val );
743 case 0: fe_fec = FEC_NONE; break;
744 case 1: fe_fec = FEC_1_2; break;
745 case 2: fe_fec = FEC_2_3; break;
746 case 3: fe_fec = FEC_3_4; break;
747 case 4: fe_fec = FEC_4_5; break;
748 case 5: fe_fec = FEC_5_6; break;
749 case 6: fe_fec = FEC_6_7; break;
750 case 7: fe_fec = FEC_7_8; break;
751 case 8: fe_fec = FEC_8_9; break;
752 case 9: fe_fec = FEC_AUTO; break;
756 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
762 static fe_modulation_t DecodeModulationQAM( access_t *p_access )
764 switch( var_GetInteger( p_access, "dvb-modulation" ) )
766 case 0: return QAM_AUTO;
767 case 16: return QAM_16;
768 case 32: return QAM_32;
769 case 64: return QAM_64;
770 case 128: return QAM_128;
771 case 256: return QAM_256;
773 msg_Dbg( p_access, "QAM modulation not set, using auto");
777 static fe_modulation_t DecodeModulationOFDM( access_t *p_access )
779 switch( var_GetInteger( p_access, "dvb-modulation" ) )
781 case -1: return QPSK;
782 case 0: return QAM_AUTO;
783 case 16: return QAM_16;
784 case 32: return QAM_32;
785 case 64: return QAM_64;
786 case 128: return QAM_128;
787 case 256: return QAM_256;
789 msg_Dbg( p_access, "OFDM modulation not set, using QAM auto");
793 static fe_modulation_t DecodeModulationATSC( access_t *p_access )
795 switch( var_GetInteger( p_access, "dvb-modulation" ) )
797 case 8: return VSB_8;
798 case 16: return VSB_16;
800 msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
805 /*****************************************************************************
806 * FrontendSetQPSK : controls the FE device
807 *****************************************************************************/
808 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
811 fe_sec_voltage_t fe_voltage;
813 var_Get( p_access, "dvb-voltage", &val );
814 msg_Dbg( p_access, "using voltage=%d", val.i_int );
818 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
819 case 13: fe_voltage = SEC_VOLTAGE_13; break;
820 case 18: fe_voltage = SEC_VOLTAGE_18; break;
822 fe_voltage = SEC_VOLTAGE_OFF;
823 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
829 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
832 fe_sec_tone_mode_t fe_tone;
834 var_Get( p_access, "dvb-tone", &val );
835 msg_Dbg( p_access, "using tone=%d", val.i_int );
839 case 0: fe_tone = SEC_TONE_OFF; break;
840 case 1: fe_tone = SEC_TONE_ON; break;
842 fe_tone = SEC_TONE_OFF;
843 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
851 struct dvb_diseqc_master_cmd cmd;
855 static int DoDiseqc( access_t *p_access )
857 access_sys_t *p_sys = p_access->p_sys;
859 int i_frequency, i_lnb_slof;
860 fe_sec_voltage_t fe_voltage;
861 fe_sec_tone_mode_t fe_tone;
864 var_Get( p_access, "dvb-frequency", &val );
865 i_frequency = val.i_int;
866 var_Get( p_access, "dvb-lnb-slof", &val );
867 i_lnb_slof = val.i_int;
869 var_Get( p_access, "dvb-tone", &val );
870 if( val.i_int == -1 /* auto */ )
872 if( i_frequency >= i_lnb_slof )
876 var_Set( p_access, "dvb-tone", val );
879 fe_voltage = DecodeVoltage( p_access );
880 fe_tone = DecodeTone( p_access );
882 /* Switch off continuous tone. */
883 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
885 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
886 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
890 /* Configure LNB voltage. */
891 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
893 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
898 var_Get( p_access, "dvb-high-voltage", &val );
899 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
900 val.b_bool )) < 0 && val.b_bool )
903 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
907 /* Wait for at least 15 ms. */
910 var_Get( p_access, "dvb-satno", &val );
911 if( val.i_int > 0 && val.i_int < 5 )
913 /* digital satellite equipment control,
914 * specification is available from http://www.eutelsat.com/
917 /* 1.x compatible equipment */
918 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
920 /* param: high nibble: reset bits, low nibble set bits,
921 * bits are: option, position, polarization, band
923 cmd.cmd.msg[3] = 0xf0 /* reset bits */
924 | (((val.i_int - 1) * 4) & 0xc)
925 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
926 | (fe_tone == SEC_TONE_ON ? 1 : 0);
928 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
931 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
936 msleep(15000 + cmd.wait * 1000);
938 /* A or B simple diseqc ("diseqc-compatible") */
939 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
940 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
942 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
950 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
952 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
953 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
961 static int FrontendSetQPSK( access_t *p_access )
963 access_sys_t *p_sys = p_access->p_sys;
964 struct dvb_frontend_parameters fep;
967 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
969 /* Prepare the fep structure */
970 var_Get( p_access, "dvb-frequency", &val );
971 i_frequency = val.i_int;
973 var_Get( p_access, "dvb-lnb-lof1", &val );
974 if ( val.i_int == 0 )
976 /* Automatic mode. */
977 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
979 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
982 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
984 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
985 i_lnb_lof1 = 3650000;
987 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
989 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
991 i_lnb_lof1 = 5150000;
993 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
995 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
997 i_lnb_lof1 = 5950000;
999 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1001 msg_Dbg( p_access, "frequency %d is in Ku-band",
1003 i_lnb_lof1 = 9750000;
1004 i_lnb_lof2 = 10600000;
1005 i_lnb_slof = 11700000;
1009 msg_Err( p_access, "frequency %d is out of any known band",
1011 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1012 "oscillator frequency" );
1013 return VLC_EGENERIC;
1015 val.i_int = i_lnb_lof1;
1016 var_Set( p_access, "dvb-lnb-lof1", val );
1017 val.i_int = i_lnb_lof2;
1018 var_Set( p_access, "dvb-lnb-lof2", val );
1019 val.i_int = i_lnb_slof;
1020 var_Set( p_access, "dvb-lnb-slof", val );
1024 i_lnb_lof1 = val.i_int;
1025 var_Get( p_access, "dvb-lnb-lof2", &val );
1026 i_lnb_lof2 = val.i_int;
1027 var_Get( p_access, "dvb-lnb-slof", &val );
1028 i_lnb_slof = val.i_int;
1031 if( i_lnb_slof && i_frequency >= i_lnb_slof )
1033 i_frequency -= i_lnb_lof2;
1037 i_frequency -= i_lnb_lof1;
1039 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1041 fep.inversion = DecodeInversion( p_access );
1043 var_Get( p_access, "dvb-srate", &val );
1044 fep.u.qpsk.symbol_rate = val.i_int;
1046 var_Get( p_access, "dvb-fec", &val );
1047 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
1049 if( DoDiseqc( p_access ) < 0 )
1051 return VLC_EGENERIC;
1054 /* Empty the event queue */
1057 struct dvb_frontend_event event;
1058 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1059 && errno == EWOULDBLOCK )
1063 /* Now send it all to the frontend device */
1064 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1066 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1067 return VLC_EGENERIC;
1073 /*****************************************************************************
1074 * FrontendSetQAM : controls the FE device
1075 *****************************************************************************/
1076 static int FrontendSetQAM( access_t *p_access )
1078 access_sys_t *p_sys = p_access->p_sys;
1079 struct dvb_frontend_parameters fep;
1083 /* Prepare the fep structure */
1085 var_Get( p_access, "dvb-frequency", &val );
1086 fep.frequency = val.i_int;
1088 fep.inversion = DecodeInversion( p_access );
1090 var_Get( p_access, "dvb-srate", &val );
1091 fep.u.qam.symbol_rate = val.i_int;
1093 var_Get( p_access, "dvb-fec", &val );
1094 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
1096 fep.u.qam.modulation = DecodeModulationQAM( p_access );
1098 /* Empty the event queue */
1101 struct dvb_frontend_event event;
1102 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1103 && errno == EWOULDBLOCK )
1107 /* Now send it all to the frontend device */
1108 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1110 msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1111 return VLC_EGENERIC;
1117 /*****************************************************************************
1118 * FrontendSetOFDM : controls the FE device
1119 *****************************************************************************/
1120 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1123 fe_bandwidth_t fe_bandwidth = 0;
1125 var_Get( p_access, "dvb-bandwidth", &val );
1126 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
1130 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1131 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1132 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1133 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1135 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1136 fe_bandwidth = BANDWIDTH_AUTO;
1139 return fe_bandwidth;
1142 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1145 fe_transmit_mode_t fe_transmission = 0;
1147 var_Get( p_access, "dvb-transmission", &val );
1148 msg_Dbg( p_access, "using transmission=%d", val.i_int );
1152 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1153 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1154 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1156 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1157 fe_transmission = TRANSMISSION_MODE_AUTO;
1160 return fe_transmission;
1163 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1166 fe_guard_interval_t fe_guard = 0;
1168 var_Get( p_access, "dvb-guard", &val );
1169 msg_Dbg( p_access, "using guard=%d", val.i_int );
1173 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1174 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1175 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1176 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1177 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1179 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1180 fe_guard = GUARD_INTERVAL_AUTO;
1186 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1189 fe_hierarchy_t fe_hierarchy = 0;
1191 var_Get( p_access, "dvb-hierarchy", &val );
1192 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
1196 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1197 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1198 case 1: fe_hierarchy = HIERARCHY_1; break;
1199 case 2: fe_hierarchy = HIERARCHY_2; break;
1200 case 4: fe_hierarchy = HIERARCHY_4; break;
1202 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1203 fe_hierarchy = HIERARCHY_AUTO;
1206 return fe_hierarchy;
1209 static int FrontendSetOFDM( access_t * p_access )
1211 access_sys_t *p_sys = p_access->p_sys;
1212 struct dvb_frontend_parameters fep;
1216 /* Prepare the fep structure */
1218 var_Get( p_access, "dvb-frequency", &val );
1219 fep.frequency = val.i_int;
1221 fep.inversion = DecodeInversion( p_access );
1223 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1224 var_Get( p_access, "dvb-code-rate-hp", &val );
1225 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
1226 var_Get( p_access, "dvb-code-rate-lp", &val );
1227 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
1228 fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1229 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1230 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1231 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1233 /* Empty the event queue */
1236 struct dvb_frontend_event event;
1237 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1238 && errno == EWOULDBLOCK )
1242 /* Now send it all to the frontend device */
1243 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1245 msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1252 /*****************************************************************************
1253 * FrontendSetATSC : controls the FE device
1254 *****************************************************************************/
1255 static int FrontendSetATSC( access_t *p_access )
1257 access_sys_t *p_sys = p_access->p_sys;
1258 struct dvb_frontend_parameters fep;
1262 /* Prepare the fep structure */
1264 var_Get( p_access, "dvb-frequency", &val );
1265 fep.frequency = val.i_int;
1267 fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1269 /* Empty the event queue */
1272 struct dvb_frontend_event event;
1273 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1274 && errno == EWOULDBLOCK )
1278 /* Now send it all to the frontend device */
1279 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1281 msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1282 return VLC_EGENERIC;
1293 /*****************************************************************************
1294 * DMXSetFilter : controls the demux to add a filter
1295 *****************************************************************************/
1296 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1298 struct dmx_pes_filter_params s_filter_params;
1300 unsigned int i_adapter, i_device;
1304 var_Get( p_access, "dvb-adapter", &val );
1305 i_adapter = val.i_int;
1306 var_Get( p_access, "dvb-device", &val );
1307 i_device = val.i_int;
1309 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1310 >= (int)sizeof(dmx) )
1312 msg_Err( p_access, "snprintf() truncated string for DMX" );
1313 dmx[sizeof(dmx) - 1] = '\0';
1316 msg_Dbg( p_access, "Opening device %s", dmx );
1317 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1319 msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1320 return VLC_EGENERIC;
1323 /* We fill the DEMUX structure : */
1324 s_filter_params.pid = i_pid;
1325 s_filter_params.input = DMX_IN_FRONTEND;
1326 s_filter_params.output = DMX_OUT_TS_TAP;
1327 s_filter_params.flags = DMX_IMMEDIATE_START;
1330 { /* First device */
1332 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1333 s_filter_params.pes_type = DMX_PES_VIDEO0;
1336 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1337 s_filter_params.pes_type = DMX_PES_AUDIO0;
1340 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1341 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1344 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1345 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1348 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1349 s_filter_params.pes_type = DMX_PES_PCR0;
1353 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1354 s_filter_params.pes_type = DMX_PES_VIDEO1;
1357 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1358 s_filter_params.pes_type = DMX_PES_AUDIO1;
1361 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1362 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1365 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1366 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1369 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1370 s_filter_params.pes_type = DMX_PES_PCR1;
1374 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1375 s_filter_params.pes_type = DMX_PES_VIDEO2;
1378 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1379 s_filter_params.pes_type = DMX_PES_AUDIO2;
1382 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1383 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1386 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1387 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1390 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1391 s_filter_params.pes_type = DMX_PES_PCR2;
1395 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1396 s_filter_params.pes_type = DMX_PES_VIDEO3;
1399 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1400 s_filter_params.pes_type = DMX_PES_AUDIO3;
1403 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1404 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1407 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1408 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1411 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1412 s_filter_params.pes_type = DMX_PES_PCR3;
1414 /* Usually used by Nova cards */
1417 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1418 s_filter_params.pes_type = DMX_PES_OTHER;
1422 /* We then give the order to the device : */
1423 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1425 msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1426 return VLC_EGENERIC;
1431 /*****************************************************************************
1432 * DMXUnsetFilter : removes a filter
1433 *****************************************************************************/
1434 int DMXUnsetFilter( access_t * p_access, int i_fd )
1438 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1440 msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1444 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1454 /*****************************************************************************
1456 *****************************************************************************/
1457 int DVROpen( access_t * p_access )
1459 access_sys_t *p_sys = p_access->p_sys;
1460 unsigned int i_adapter, i_device;
1464 var_Get( p_access, "dvb-adapter", &val );
1465 i_adapter = val.i_int;
1466 var_Get( p_access, "dvb-device", &val );
1467 i_device = val.i_int;
1469 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1470 >= (int)sizeof(dvr) )
1472 msg_Err( p_access, "snprintf() truncated string for DVR" );
1473 dvr[sizeof(dvr) - 1] = '\0';
1476 msg_Dbg( p_access, "Opening device %s", dvr );
1477 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1479 msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1480 return VLC_EGENERIC;
1483 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1485 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1491 /*****************************************************************************
1493 *****************************************************************************/
1494 void DVRClose( access_t * p_access )
1496 access_sys_t *p_sys = p_access->p_sys;
1498 close( p_sys->i_handle );
1506 /*****************************************************************************
1508 *****************************************************************************/
1509 int CAMOpen( access_t *p_access )
1511 access_sys_t *p_sys = p_access->p_sys;
1513 int i_adapter, i_device;
1516 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1517 i_device = var_GetInteger( p_access, "dvb-device" );
1519 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1521 msg_Err( p_access, "snprintf() truncated string for CA" );
1522 ca[sizeof(ca) - 1] = '\0';
1524 memset( &caps, 0, sizeof( ca_caps_t ));
1526 msg_Dbg( p_access, "Opening device %s", ca );
1527 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1529 msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1530 p_sys->i_ca_handle = 0;
1531 return VLC_EGENERIC;
1534 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1536 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1537 close( p_sys->i_ca_handle );
1538 p_sys->i_ca_handle = 0;
1539 return VLC_EGENERIC;
1542 /* Output CA capabilities */
1543 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1544 caps.slot_num == 1 ? "slot" : "slots" );
1545 if ( caps.slot_type & CA_CI )
1546 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1547 if ( caps.slot_type & CA_CI_LINK )
1548 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1549 if ( caps.slot_type & CA_CI_PHYS )
1550 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1551 if ( caps.slot_type & CA_DESCR )
1552 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1553 if ( caps.slot_type & CA_SC )
1554 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1556 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1557 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1558 if ( caps.descr_type & CA_ECD )
1559 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1560 if ( caps.descr_type & CA_NDS )
1561 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1562 if ( caps.descr_type & CA_DSS )
1563 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1565 if ( caps.slot_num == 0 )
1567 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1568 close( p_sys->i_ca_handle );
1569 p_sys->i_ca_handle = 0;
1570 return VLC_EGENERIC;
1573 if( caps.slot_type & CA_CI_LINK )
1575 p_sys->i_ca_type = CA_CI_LINK;
1577 else if( caps.slot_type & CA_CI )
1579 p_sys->i_ca_type = CA_CI;
1583 p_sys->i_ca_type = -1;
1584 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1585 close( p_sys->i_ca_handle );
1586 p_sys->i_ca_handle = 0;
1587 return VLC_EGENERIC;
1590 p_sys->i_nb_slots = caps.slot_num;
1591 memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1592 memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1593 memset( p_sys->pb_slot_mmi_undisplayed, 0,
1594 sizeof(bool) * MAX_CI_SLOTS );
1596 return en50221_Init( p_access );
1599 /*****************************************************************************
1601 *****************************************************************************/
1602 int CAMPoll( access_t * p_access )
1604 access_sys_t *p_sys = p_access->p_sys;
1605 int i_ret = VLC_EGENERIC;
1607 if ( p_sys->i_ca_handle == 0 )
1609 return VLC_EGENERIC;
1612 switch( p_sys->i_ca_type )
1615 i_ret = en50221_Poll( p_access );
1618 i_ret = VLC_SUCCESS;
1621 msg_Err( p_access, "CAMPoll: This should not happen" );
1629 /*****************************************************************************
1631 *****************************************************************************/
1632 void CAMStatus( access_t * p_access )
1634 access_sys_t *p_sys = p_access->p_sys;
1639 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1641 /* Check if we have an undisplayed MMI message : in that case we ignore
1642 * the user input to avoid confusing the CAM. */
1643 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1645 if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1647 p_sys->psz_request = NULL;
1649 "ignoring user request because of a new MMI object" );
1655 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1657 /* We have a mission to accomplish. */
1658 en50221_mmi_object_t mmi_object;
1659 char *psz_request = p_sys->psz_request;
1660 char psz_value[255];
1664 p_sys->psz_request = NULL;
1666 if ( HTTPExtractValue( psz_request, "slot", psz_value,
1667 sizeof(psz_value) ) == NULL )
1669 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1672 i_slot = atoi(psz_value);
1674 if ( HTTPExtractValue( psz_request, "open", psz_value,
1675 sizeof(psz_value) ) != NULL )
1677 en50221_OpenMMI( p_access, i_slot );
1681 if ( HTTPExtractValue( psz_request, "close", psz_value,
1682 sizeof(psz_value) ) != NULL )
1684 en50221_CloseMMI( p_access, i_slot );
1688 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1689 sizeof(psz_value) ) == NULL )
1694 if ( HTTPExtractValue( psz_request, "type", psz_value,
1695 sizeof(psz_value) ) == NULL )
1697 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1701 if ( !strcmp( psz_value, "enq" ) )
1703 mmi_object.i_object_type = EN50221_MMI_ANSW;
1704 mmi_object.u.answ.b_ok = b_ok;
1705 if ( b_ok == false )
1707 mmi_object.u.answ.psz_answ = strdup("");
1711 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1712 sizeof(psz_value) ) == NULL )
1714 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1718 mmi_object.u.answ.psz_answ = strdup(psz_value);
1723 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1724 if ( b_ok == false )
1726 mmi_object.u.menu_answ.i_choice = 0;
1730 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1731 sizeof(psz_value) ) == NULL )
1732 mmi_object.u.menu_answ.i_choice = 0;
1734 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1738 en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1742 /* Check that we have all necessary MMI information. */
1743 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1745 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1749 p = p_sys->psz_mmi_info = malloc( 10000 );
1751 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1754 strerror_r( errno, buf, sizeof( buf ) );
1755 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1759 /* Output CA capabilities */
1760 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1761 caps.slot_num == 1 ? "slot" : "slots" );
1762 #define CHECK_CAPS( x, s ) \
1763 if ( caps.slot_type & (CA_##x) ) \
1764 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1766 CHECK_CAPS( CI, "CI high level interface" );
1767 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1768 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1769 CHECK_CAPS( DESCR, "built-in descrambler" );
1770 CHECK_CAPS( SC, "simple smartcard interface" );
1773 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1774 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1775 #define CHECK_DESC( x ) \
1776 if ( caps.descr_type & (CA_##x) ) \
1777 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1784 p += sprintf( p, "</table>" );
1786 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1788 ca_slot_info_t sinfo;
1790 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1791 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1794 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1797 strerror_r( errno, buf, sizeof( buf ) );
1798 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1802 #define CHECK_TYPE( x, s ) \
1803 if ( sinfo.type & (CA_##x) ) \
1804 p += sprintf( p, "%s", s );
1806 CHECK_TYPE( CI, "high level, " );
1807 CHECK_TYPE( CI_LINK, "link layer level, " );
1808 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1811 if ( sinfo.flags & CA_CI_MODULE_READY )
1813 en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1816 p += sprintf( p, "module present and ready<p>\n" );
1817 p += sprintf( p, "<form action=index.html method=get>\n" );
1818 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1821 if ( p_object == NULL )
1823 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1827 switch ( p_object->i_object_type )
1829 case EN50221_MMI_ENQ:
1830 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1831 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1832 p_object->u.enq.psz_text );
1833 if ( p_object->u.enq.b_blind == false )
1834 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1836 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1839 case EN50221_MMI_MENU:
1840 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1841 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1842 p_object->u.menu.psz_title );
1843 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1844 p_object->u.menu.psz_subtitle );
1845 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1846 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1847 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1848 p_object->u.menu.psz_bottom );
1851 case EN50221_MMI_LIST:
1852 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1853 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1854 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1855 p_object->u.menu.psz_title );
1856 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1857 p_object->u.menu.psz_subtitle );
1858 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1859 p += sprintf( p, "%s<br>\n",
1860 p_object->u.menu.ppsz_choices[i] );
1861 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1862 p_object->u.menu.psz_bottom );
1866 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1869 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1870 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1871 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1873 p += sprintf( p, "</form>\n" );
1875 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1876 p += sprintf( p, "module present, not ready<br>\n" );
1878 p += sprintf( p, "module not present<br>\n" );
1882 vlc_mutex_lock( &p_sys->httpd_mutex );
1883 p_sys->b_request_mmi_info = false;
1884 vlc_cond_signal( &p_sys->httpd_cond );
1885 vlc_mutex_unlock( &p_sys->httpd_mutex );
1889 /*****************************************************************************
1891 *****************************************************************************/
1892 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1894 access_sys_t *p_sys = p_access->p_sys;
1896 if( p_sys->i_ca_handle == 0 )
1898 dvbpsi_DeletePMT( p_pmt );
1899 return VLC_EGENERIC;
1902 en50221_SetCAPMT( p_access, p_pmt );
1907 /*****************************************************************************
1909 *****************************************************************************/
1910 void CAMClose( access_t * p_access )
1912 access_sys_t *p_sys = p_access->p_sys;
1914 en50221_End( p_access );
1916 if ( p_sys->i_ca_handle )
1918 close( p_sys->i_ca_handle );