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 0: return QAM_AUTO;
798 case 8: return VSB_8;
799 case 16: return VSB_16;
800 case 32: return QAM_32;
801 case 64: return QAM_64;
802 case 128: return QAM_128;
803 case 256: return QAM_256;
805 msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
810 /*****************************************************************************
811 * FrontendSetQPSK : controls the FE device
812 *****************************************************************************/
813 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
816 fe_sec_voltage_t fe_voltage;
818 var_Get( p_access, "dvb-voltage", &val );
819 msg_Dbg( p_access, "using voltage=%d", val.i_int );
823 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
824 case 13: fe_voltage = SEC_VOLTAGE_13; break;
825 case 18: fe_voltage = SEC_VOLTAGE_18; break;
827 fe_voltage = SEC_VOLTAGE_OFF;
828 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
834 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
837 fe_sec_tone_mode_t fe_tone;
839 var_Get( p_access, "dvb-tone", &val );
840 msg_Dbg( p_access, "using tone=%d", val.i_int );
844 case 0: fe_tone = SEC_TONE_OFF; break;
845 case 1: fe_tone = SEC_TONE_ON; break;
847 fe_tone = SEC_TONE_OFF;
848 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
856 struct dvb_diseqc_master_cmd cmd;
860 static int DoDiseqc( access_t *p_access )
862 access_sys_t *p_sys = p_access->p_sys;
864 int i_frequency, i_lnb_slof;
865 fe_sec_voltage_t fe_voltage;
866 fe_sec_tone_mode_t fe_tone;
869 var_Get( p_access, "dvb-frequency", &val );
870 i_frequency = val.i_int;
871 var_Get( p_access, "dvb-lnb-slof", &val );
872 i_lnb_slof = val.i_int;
874 var_Get( p_access, "dvb-tone", &val );
875 if( val.i_int == -1 /* auto */ )
877 if( i_frequency >= i_lnb_slof )
881 var_Set( p_access, "dvb-tone", val );
884 fe_voltage = DecodeVoltage( p_access );
885 fe_tone = DecodeTone( p_access );
887 /* Switch off continuous tone. */
888 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
890 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
891 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
895 /* Configure LNB voltage. */
896 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
898 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
903 var_Get( p_access, "dvb-high-voltage", &val );
904 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
905 val.b_bool )) < 0 && val.b_bool )
908 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
912 /* Wait for at least 15 ms. */
915 var_Get( p_access, "dvb-satno", &val );
916 if( val.i_int > 0 && val.i_int < 5 )
918 /* digital satellite equipment control,
919 * specification is available from http://www.eutelsat.com/
922 /* 1.x compatible equipment */
923 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
925 /* param: high nibble: reset bits, low nibble set bits,
926 * bits are: option, position, polarization, band
928 cmd.cmd.msg[3] = 0xf0 /* reset bits */
929 | (((val.i_int - 1) * 4) & 0xc)
930 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
931 | (fe_tone == SEC_TONE_ON ? 1 : 0);
933 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
936 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
941 msleep(15000 + cmd.wait * 1000);
943 /* A or B simple diseqc ("diseqc-compatible") */
944 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
945 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
947 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
955 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
957 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
958 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
966 static int FrontendSetQPSK( access_t *p_access )
968 access_sys_t *p_sys = p_access->p_sys;
969 struct dvb_frontend_parameters fep;
972 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
974 /* Prepare the fep structure */
975 var_Get( p_access, "dvb-frequency", &val );
976 i_frequency = val.i_int;
978 var_Get( p_access, "dvb-lnb-lof1", &val );
979 if ( val.i_int == 0 )
981 /* Automatic mode. */
982 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
984 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
987 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
989 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
990 i_lnb_lof1 = 3650000;
992 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
994 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
996 i_lnb_lof1 = 5150000;
998 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
1000 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
1002 i_lnb_lof1 = 5950000;
1004 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1006 msg_Dbg( p_access, "frequency %d is in Ku-band",
1008 i_lnb_lof1 = 9750000;
1009 i_lnb_lof2 = 10600000;
1010 i_lnb_slof = 11700000;
1014 msg_Err( p_access, "frequency %d is out of any known band",
1016 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1017 "oscillator frequency" );
1018 return VLC_EGENERIC;
1020 val.i_int = i_lnb_lof1;
1021 var_Set( p_access, "dvb-lnb-lof1", val );
1022 val.i_int = i_lnb_lof2;
1023 var_Set( p_access, "dvb-lnb-lof2", val );
1024 val.i_int = i_lnb_slof;
1025 var_Set( p_access, "dvb-lnb-slof", val );
1029 i_lnb_lof1 = val.i_int;
1030 var_Get( p_access, "dvb-lnb-lof2", &val );
1031 i_lnb_lof2 = val.i_int;
1032 var_Get( p_access, "dvb-lnb-slof", &val );
1033 i_lnb_slof = val.i_int;
1036 if( i_lnb_slof && i_frequency >= i_lnb_slof )
1038 i_frequency -= i_lnb_lof2;
1042 i_frequency -= i_lnb_lof1;
1044 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1046 fep.inversion = DecodeInversion( p_access );
1048 var_Get( p_access, "dvb-srate", &val );
1049 fep.u.qpsk.symbol_rate = val.i_int;
1051 var_Get( p_access, "dvb-fec", &val );
1052 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
1054 if( DoDiseqc( p_access ) < 0 )
1056 return VLC_EGENERIC;
1059 /* Empty the event queue */
1062 struct dvb_frontend_event event;
1063 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1064 && errno == EWOULDBLOCK )
1068 /* Now send it all to the frontend device */
1069 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1071 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1072 return VLC_EGENERIC;
1078 /*****************************************************************************
1079 * FrontendSetQAM : controls the FE device
1080 *****************************************************************************/
1081 static int FrontendSetQAM( access_t *p_access )
1083 access_sys_t *p_sys = p_access->p_sys;
1084 struct dvb_frontend_parameters fep;
1088 /* Prepare the fep structure */
1090 var_Get( p_access, "dvb-frequency", &val );
1091 fep.frequency = val.i_int;
1093 fep.inversion = DecodeInversion( p_access );
1095 var_Get( p_access, "dvb-srate", &val );
1096 fep.u.qam.symbol_rate = val.i_int;
1098 var_Get( p_access, "dvb-fec", &val );
1099 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
1101 fep.u.qam.modulation = DecodeModulationQAM( p_access );
1103 /* Empty the event queue */
1106 struct dvb_frontend_event event;
1107 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1108 && errno == EWOULDBLOCK )
1112 /* Now send it all to the frontend device */
1113 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1115 msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1116 return VLC_EGENERIC;
1122 /*****************************************************************************
1123 * FrontendSetOFDM : controls the FE device
1124 *****************************************************************************/
1125 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1128 fe_bandwidth_t fe_bandwidth = 0;
1130 var_Get( p_access, "dvb-bandwidth", &val );
1131 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
1135 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1136 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1137 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1138 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1140 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1141 fe_bandwidth = BANDWIDTH_AUTO;
1144 return fe_bandwidth;
1147 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1150 fe_transmit_mode_t fe_transmission = 0;
1152 var_Get( p_access, "dvb-transmission", &val );
1153 msg_Dbg( p_access, "using transmission=%d", val.i_int );
1157 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1158 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1159 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1161 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1162 fe_transmission = TRANSMISSION_MODE_AUTO;
1165 return fe_transmission;
1168 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1171 fe_guard_interval_t fe_guard = 0;
1173 var_Get( p_access, "dvb-guard", &val );
1174 msg_Dbg( p_access, "using guard=%d", val.i_int );
1178 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1179 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1180 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1181 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1182 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1184 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1185 fe_guard = GUARD_INTERVAL_AUTO;
1191 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1194 fe_hierarchy_t fe_hierarchy = 0;
1196 var_Get( p_access, "dvb-hierarchy", &val );
1197 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
1201 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1202 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1203 case 1: fe_hierarchy = HIERARCHY_1; break;
1204 case 2: fe_hierarchy = HIERARCHY_2; break;
1205 case 4: fe_hierarchy = HIERARCHY_4; break;
1207 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1208 fe_hierarchy = HIERARCHY_AUTO;
1211 return fe_hierarchy;
1214 static int FrontendSetOFDM( access_t * p_access )
1216 access_sys_t *p_sys = p_access->p_sys;
1217 struct dvb_frontend_parameters fep;
1221 /* Prepare the fep structure */
1223 var_Get( p_access, "dvb-frequency", &val );
1224 fep.frequency = val.i_int;
1226 fep.inversion = DecodeInversion( p_access );
1228 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1229 var_Get( p_access, "dvb-code-rate-hp", &val );
1230 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
1231 var_Get( p_access, "dvb-code-rate-lp", &val );
1232 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
1233 fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1234 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1235 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1236 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1238 /* Empty the event queue */
1241 struct dvb_frontend_event event;
1242 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1243 && errno == EWOULDBLOCK )
1247 /* Now send it all to the frontend device */
1248 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1250 msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1257 /*****************************************************************************
1258 * FrontendSetATSC : controls the FE device
1259 *****************************************************************************/
1260 static int FrontendSetATSC( access_t *p_access )
1262 access_sys_t *p_sys = p_access->p_sys;
1263 struct dvb_frontend_parameters fep;
1267 /* Prepare the fep structure */
1269 var_Get( p_access, "dvb-frequency", &val );
1270 fep.frequency = val.i_int;
1272 fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1274 /* Empty the event queue */
1277 struct dvb_frontend_event event;
1278 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1279 && errno == EWOULDBLOCK )
1283 /* Now send it all to the frontend device */
1284 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1286 msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1287 return VLC_EGENERIC;
1298 /*****************************************************************************
1299 * DMXSetFilter : controls the demux to add a filter
1300 *****************************************************************************/
1301 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1303 struct dmx_pes_filter_params s_filter_params;
1305 unsigned int i_adapter, i_device;
1309 var_Get( p_access, "dvb-adapter", &val );
1310 i_adapter = val.i_int;
1311 var_Get( p_access, "dvb-device", &val );
1312 i_device = val.i_int;
1314 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1315 >= (int)sizeof(dmx) )
1317 msg_Err( p_access, "snprintf() truncated string for DMX" );
1318 dmx[sizeof(dmx) - 1] = '\0';
1321 msg_Dbg( p_access, "Opening device %s", dmx );
1322 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1324 msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1325 return VLC_EGENERIC;
1328 /* We fill the DEMUX structure : */
1329 s_filter_params.pid = i_pid;
1330 s_filter_params.input = DMX_IN_FRONTEND;
1331 s_filter_params.output = DMX_OUT_TS_TAP;
1332 s_filter_params.flags = DMX_IMMEDIATE_START;
1335 { /* First device */
1337 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1338 s_filter_params.pes_type = DMX_PES_VIDEO0;
1341 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1342 s_filter_params.pes_type = DMX_PES_AUDIO0;
1345 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1346 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1349 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1350 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1353 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1354 s_filter_params.pes_type = DMX_PES_PCR0;
1358 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1359 s_filter_params.pes_type = DMX_PES_VIDEO1;
1362 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1363 s_filter_params.pes_type = DMX_PES_AUDIO1;
1366 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1367 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1370 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1371 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1374 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1375 s_filter_params.pes_type = DMX_PES_PCR1;
1379 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1380 s_filter_params.pes_type = DMX_PES_VIDEO2;
1383 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1384 s_filter_params.pes_type = DMX_PES_AUDIO2;
1387 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1388 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1391 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1392 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1395 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1396 s_filter_params.pes_type = DMX_PES_PCR2;
1400 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1401 s_filter_params.pes_type = DMX_PES_VIDEO3;
1404 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1405 s_filter_params.pes_type = DMX_PES_AUDIO3;
1408 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1409 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1412 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1413 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1416 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1417 s_filter_params.pes_type = DMX_PES_PCR3;
1419 /* Usually used by Nova cards */
1422 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1423 s_filter_params.pes_type = DMX_PES_OTHER;
1427 /* We then give the order to the device : */
1428 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1430 msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1431 return VLC_EGENERIC;
1436 /*****************************************************************************
1437 * DMXUnsetFilter : removes a filter
1438 *****************************************************************************/
1439 int DMXUnsetFilter( access_t * p_access, int i_fd )
1443 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1445 msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1449 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1459 /*****************************************************************************
1461 *****************************************************************************/
1462 int DVROpen( access_t * p_access )
1464 access_sys_t *p_sys = p_access->p_sys;
1465 unsigned int i_adapter, i_device;
1469 var_Get( p_access, "dvb-adapter", &val );
1470 i_adapter = val.i_int;
1471 var_Get( p_access, "dvb-device", &val );
1472 i_device = val.i_int;
1474 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1475 >= (int)sizeof(dvr) )
1477 msg_Err( p_access, "snprintf() truncated string for DVR" );
1478 dvr[sizeof(dvr) - 1] = '\0';
1481 msg_Dbg( p_access, "Opening device %s", dvr );
1482 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1484 msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1485 return VLC_EGENERIC;
1488 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1490 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1496 /*****************************************************************************
1498 *****************************************************************************/
1499 void DVRClose( access_t * p_access )
1501 access_sys_t *p_sys = p_access->p_sys;
1503 close( p_sys->i_handle );
1511 /*****************************************************************************
1513 *****************************************************************************/
1514 int CAMOpen( access_t *p_access )
1516 access_sys_t *p_sys = p_access->p_sys;
1518 int i_adapter, i_device;
1521 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1522 i_device = var_GetInteger( p_access, "dvb-device" );
1524 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1526 msg_Err( p_access, "snprintf() truncated string for CA" );
1527 ca[sizeof(ca) - 1] = '\0';
1529 memset( &caps, 0, sizeof( ca_caps_t ));
1531 msg_Dbg( p_access, "Opening device %s", ca );
1532 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1534 msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1535 p_sys->i_ca_handle = 0;
1536 return VLC_EGENERIC;
1539 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1541 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1542 close( p_sys->i_ca_handle );
1543 p_sys->i_ca_handle = 0;
1544 return VLC_EGENERIC;
1547 /* Output CA capabilities */
1548 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1549 caps.slot_num == 1 ? "slot" : "slots" );
1550 if ( caps.slot_type & CA_CI )
1551 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1552 if ( caps.slot_type & CA_CI_LINK )
1553 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1554 if ( caps.slot_type & CA_CI_PHYS )
1555 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1556 if ( caps.slot_type & CA_DESCR )
1557 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1558 if ( caps.slot_type & CA_SC )
1559 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1561 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1562 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1563 if ( caps.descr_type & CA_ECD )
1564 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1565 if ( caps.descr_type & CA_NDS )
1566 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1567 if ( caps.descr_type & CA_DSS )
1568 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1570 if ( caps.slot_num == 0 )
1572 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1573 close( p_sys->i_ca_handle );
1574 p_sys->i_ca_handle = 0;
1575 return VLC_EGENERIC;
1578 if( caps.slot_type & CA_CI_LINK )
1580 p_sys->i_ca_type = CA_CI_LINK;
1582 else if( caps.slot_type & CA_CI )
1584 p_sys->i_ca_type = CA_CI;
1588 p_sys->i_ca_type = -1;
1589 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1590 close( p_sys->i_ca_handle );
1591 p_sys->i_ca_handle = 0;
1592 return VLC_EGENERIC;
1595 p_sys->i_nb_slots = caps.slot_num;
1596 memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1597 memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1598 memset( p_sys->pb_slot_mmi_undisplayed, 0,
1599 sizeof(bool) * MAX_CI_SLOTS );
1601 return en50221_Init( p_access );
1604 /*****************************************************************************
1606 *****************************************************************************/
1607 int CAMPoll( access_t * p_access )
1609 access_sys_t *p_sys = p_access->p_sys;
1610 int i_ret = VLC_EGENERIC;
1612 if ( p_sys->i_ca_handle == 0 )
1614 return VLC_EGENERIC;
1617 switch( p_sys->i_ca_type )
1620 i_ret = en50221_Poll( p_access );
1623 i_ret = VLC_SUCCESS;
1626 msg_Err( p_access, "CAMPoll: This should not happen" );
1634 /*****************************************************************************
1636 *****************************************************************************/
1637 void CAMStatus( access_t * p_access )
1639 access_sys_t *p_sys = p_access->p_sys;
1644 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1646 /* Check if we have an undisplayed MMI message : in that case we ignore
1647 * the user input to avoid confusing the CAM. */
1648 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1650 if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1652 p_sys->psz_request = NULL;
1654 "ignoring user request because of a new MMI object" );
1660 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1662 /* We have a mission to accomplish. */
1663 en50221_mmi_object_t mmi_object;
1664 char *psz_request = p_sys->psz_request;
1665 char psz_value[255];
1669 p_sys->psz_request = NULL;
1671 if ( HTTPExtractValue( psz_request, "slot", psz_value,
1672 sizeof(psz_value) ) == NULL )
1674 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1677 i_slot = atoi(psz_value);
1679 if ( HTTPExtractValue( psz_request, "open", psz_value,
1680 sizeof(psz_value) ) != NULL )
1682 en50221_OpenMMI( p_access, i_slot );
1686 if ( HTTPExtractValue( psz_request, "close", psz_value,
1687 sizeof(psz_value) ) != NULL )
1689 en50221_CloseMMI( p_access, i_slot );
1693 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1694 sizeof(psz_value) ) == NULL )
1699 if ( HTTPExtractValue( psz_request, "type", psz_value,
1700 sizeof(psz_value) ) == NULL )
1702 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1706 if ( !strcmp( psz_value, "enq" ) )
1708 mmi_object.i_object_type = EN50221_MMI_ANSW;
1709 mmi_object.u.answ.b_ok = b_ok;
1710 if ( b_ok == false )
1712 mmi_object.u.answ.psz_answ = strdup("");
1716 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1717 sizeof(psz_value) ) == NULL )
1719 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1723 mmi_object.u.answ.psz_answ = strdup(psz_value);
1728 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1729 if ( b_ok == false )
1731 mmi_object.u.menu_answ.i_choice = 0;
1735 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1736 sizeof(psz_value) ) == NULL )
1737 mmi_object.u.menu_answ.i_choice = 0;
1739 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1743 en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1747 /* Check that we have all necessary MMI information. */
1748 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1750 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1754 p = p_sys->psz_mmi_info = malloc( 10000 );
1756 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1759 strerror_r( errno, buf, sizeof( buf ) );
1760 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1764 /* Output CA capabilities */
1765 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1766 caps.slot_num == 1 ? "slot" : "slots" );
1767 #define CHECK_CAPS( x, s ) \
1768 if ( caps.slot_type & (CA_##x) ) \
1769 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1771 CHECK_CAPS( CI, "CI high level interface" );
1772 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1773 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1774 CHECK_CAPS( DESCR, "built-in descrambler" );
1775 CHECK_CAPS( SC, "simple smartcard interface" );
1778 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1779 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1780 #define CHECK_DESC( x ) \
1781 if ( caps.descr_type & (CA_##x) ) \
1782 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1789 p += sprintf( p, "</table>" );
1791 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1793 ca_slot_info_t sinfo;
1795 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1796 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1799 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1802 strerror_r( errno, buf, sizeof( buf ) );
1803 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1807 #define CHECK_TYPE( x, s ) \
1808 if ( sinfo.type & (CA_##x) ) \
1809 p += sprintf( p, "%s", s );
1811 CHECK_TYPE( CI, "high level, " );
1812 CHECK_TYPE( CI_LINK, "link layer level, " );
1813 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1816 if ( sinfo.flags & CA_CI_MODULE_READY )
1818 en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1821 p += sprintf( p, "module present and ready<p>\n" );
1822 p += sprintf( p, "<form action=index.html method=get>\n" );
1823 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1826 if ( p_object == NULL )
1828 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1832 switch ( p_object->i_object_type )
1834 case EN50221_MMI_ENQ:
1835 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1836 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1837 p_object->u.enq.psz_text );
1838 if ( p_object->u.enq.b_blind == false )
1839 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1841 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1844 case EN50221_MMI_MENU:
1845 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1846 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1847 p_object->u.menu.psz_title );
1848 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1849 p_object->u.menu.psz_subtitle );
1850 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1851 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1852 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1853 p_object->u.menu.psz_bottom );
1856 case EN50221_MMI_LIST:
1857 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1858 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1859 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1860 p_object->u.menu.psz_title );
1861 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1862 p_object->u.menu.psz_subtitle );
1863 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1864 p += sprintf( p, "%s<br>\n",
1865 p_object->u.menu.ppsz_choices[i] );
1866 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1867 p_object->u.menu.psz_bottom );
1871 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1874 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1875 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1876 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1878 p += sprintf( p, "</form>\n" );
1880 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1881 p += sprintf( p, "module present, not ready<br>\n" );
1883 p += sprintf( p, "module not present<br>\n" );
1887 vlc_mutex_lock( &p_sys->httpd_mutex );
1888 p_sys->b_request_mmi_info = false;
1889 vlc_cond_signal( &p_sys->httpd_cond );
1890 vlc_mutex_unlock( &p_sys->httpd_mutex );
1894 /*****************************************************************************
1896 *****************************************************************************/
1897 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1899 access_sys_t *p_sys = p_access->p_sys;
1901 if( p_sys->i_ca_handle == 0 )
1903 dvbpsi_DeletePMT( p_pmt );
1904 return VLC_EGENERIC;
1907 en50221_SetCAPMT( p_access, p_pmt );
1912 /*****************************************************************************
1914 *****************************************************************************/
1915 void CAMClose( access_t * p_access )
1917 access_sys_t *p_sys = p_access->p_sys;
1919 en50221_End( p_access );
1921 if ( p_sys->i_ca_handle )
1923 close( p_sys->i_ca_handle );