1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2005 the VideoLAN team
6 * Authors: Damien Lucas <nitrox@via.ecp.fr>
7 * Johan Bilien <jobi@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
9 * Christopher Ross <chris@tebibyte.org>
10 * Christophe Massiot <massiot@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_access.h>
33 #include <sys/ioctl.h>
36 #include <sys/types.h>
43 /* DVB Card Drivers */
44 #include <linux/dvb/version.h>
45 #include <linux/dvb/dmx.h>
46 #include <linux/dvb/frontend.h>
47 #include <linux/dvb/ca.h>
49 /* Include dvbpsi headers */
50 #ifdef HAVE_DVBPSI_DR_H
51 # include <dvbpsi/dvbpsi.h>
52 # include <dvbpsi/descriptor.h>
53 # include <dvbpsi/pat.h>
54 # include <dvbpsi/pmt.h>
55 # include <dvbpsi/dr.h>
56 # include <dvbpsi/psi.h>
57 # include <dvbpsi/demux.h>
58 # include <dvbpsi/sdt.h>
61 # include "descriptor.h"
62 # include "tables/pat.h"
63 # include "tables/pmt.h"
64 # include "descriptors/dr.h"
67 # include "tables/sdt.h"
71 # include <vlc_httpd.h>
81 fe_status_t i_last_status;
82 struct dvb_frontend_info info;
85 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
87 /* Local prototypes */
88 static int FrontendInfo( access_t * );
89 static int FrontendSetQPSK( access_t * );
90 static int FrontendSetQAM( access_t * );
91 static int FrontendSetOFDM( access_t * );
92 static int FrontendSetATSC( access_t * );
94 /*****************************************************************************
95 * FrontendOpen : Determine frontend device information and capabilities
96 *****************************************************************************/
97 int FrontendOpen( access_t *p_access )
99 access_sys_t *p_sys = p_access->p_sys;
100 frontend_t * p_frontend;
101 unsigned int i_adapter, i_device;
105 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
106 i_device = var_GetInteger( p_access, "dvb-device" );
107 b_probe = var_GetBool( p_access, "dvb-probe" );
109 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
111 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
112 frontend[sizeof(frontend) - 1] = '\0';
115 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
119 msg_Dbg( p_access, "Opening device %s", frontend );
120 if( (p_sys->i_frontend_handle = vlc_open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
122 msg_Err( p_access, "FrontEndOpen: opening device failed (%m)" );
129 const char * psz_expected = NULL;
130 const char * psz_real;
132 if( FrontendInfo( p_access ) < 0 )
134 close( p_sys->i_frontend_handle );
139 switch( p_frontend->info.type )
154 psz_real = "unknown";
158 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
159 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
160 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
161 (p_frontend->info.type != FE_QPSK) )
163 psz_expected = "DVB-S";
165 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
166 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
167 (p_frontend->info.type != FE_QAM) )
169 psz_expected = "DVB-C";
171 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
172 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
173 (p_frontend->info.type != FE_OFDM) )
175 psz_expected = "DVB-T";
178 if( (!strncmp( p_access->psz_access, "usdigital", 9 ) ||
179 !strncmp( p_access->psz_access, "atsc", 4 ) ) &&
180 (p_frontend->info.type != FE_ATSC) )
182 psz_expected = "ATSC";
185 if( psz_expected != NULL )
187 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
188 psz_expected, psz_real );
189 close( p_sys->i_frontend_handle );
194 else /* no frontend probing is done so use default border values. */
196 msg_Dbg( p_access, "using default values for frontend info" );
198 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
199 p_frontend->info.type = FE_QPSK;
200 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
201 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
202 p_frontend->info.type = FE_QPSK;
203 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
204 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
205 p_frontend->info.type = FE_QAM;
206 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
207 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
208 p_frontend->info.type = FE_OFDM;
209 else if( !strncmp( p_access->psz_access, "usdigital", 9 ) ||
210 !strncmp( p_access->psz_access, "atsc", 4 ) )
211 p_frontend->info.type = FE_ATSC;
217 /*****************************************************************************
218 * FrontendClose : Close the frontend
219 *****************************************************************************/
220 void FrontendClose( access_t *p_access )
222 access_sys_t *p_sys = p_access->p_sys;
224 if( p_sys->p_frontend )
226 close( p_sys->i_frontend_handle );
227 free( p_sys->p_frontend );
229 p_sys->p_frontend = NULL;
233 /*****************************************************************************
234 * FrontendSet : Tune !
235 *****************************************************************************/
236 int FrontendSet( access_t *p_access )
238 access_sys_t *p_sys = p_access->p_sys;
240 switch( p_sys->p_frontend->info.type )
244 if( FrontendSetQPSK( p_access ) < 0 )
246 msg_Err( p_access, "DVB-S: tuning failed" );
253 if( FrontendSetQAM( p_access ) < 0 )
255 msg_Err( p_access, "DVB-C: tuning failed" );
262 if( FrontendSetOFDM( p_access ) < 0 )
264 msg_Err( p_access, "DVB-T: tuning failed" );
271 if( FrontendSetATSC( p_access ) < 0 )
273 msg_Err( p_access, "ATSC: tuning failed" );
279 msg_Err( p_access, "Could not determine frontend type on %s",
280 p_sys->p_frontend->info.name );
283 p_sys->p_frontend->i_last_status = 0;
284 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
288 /*****************************************************************************
289 * FrontendPoll : Poll for frontend events
290 *****************************************************************************/
291 void FrontendPoll( access_t *p_access )
293 access_sys_t *p_sys = p_access->p_sys;
294 frontend_t * p_frontend = p_sys->p_frontend;
295 struct dvb_frontend_event event;
296 fe_status_t i_status, i_diff;
300 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
304 if( errno == EWOULDBLOCK )
305 return; /* no more events */
307 msg_Err( p_access, "reading frontend event failed (%d): %m",
312 i_status = event.status;
313 i_diff = i_status ^ p_frontend->i_last_status;
314 p_frontend->i_last_status = i_status;
319 if ( i_diff & (x) ) \
321 if ( i_status & (x) )
323 IF_UP( FE_HAS_SIGNAL )
324 msg_Dbg( p_access, "frontend has acquired signal" );
326 msg_Dbg( p_access, "frontend has lost signal" );
328 IF_UP( FE_HAS_CARRIER )
329 msg_Dbg( p_access, "frontend has acquired carrier" );
331 msg_Dbg( p_access, "frontend has lost carrier" );
333 IF_UP( FE_HAS_VITERBI )
334 msg_Dbg( p_access, "frontend has acquired stable FEC" );
336 msg_Dbg( p_access, "frontend has lost FEC" );
339 msg_Dbg( p_access, "frontend has acquired sync" );
341 msg_Dbg( p_access, "frontend has lost sync" );
345 frontend_statistic_t stat;
347 msg_Dbg( p_access, "frontend has acquired lock" );
348 p_sys->i_frontend_timeout = 0;
350 /* Read some statistics */
351 if( !FrontendGetStatistic( p_access, &stat ) )
353 if( stat.i_ber >= 0 )
354 msg_Dbg( p_access, "- Bit error rate: %d", stat.i_ber );
355 if( stat.i_signal_strenth >= 0 )
356 msg_Dbg( p_access, "- Signal strength: %d", stat.i_signal_strenth );
357 if( stat.i_snr >= 0 )
358 msg_Dbg( p_access, "- SNR: %d", stat.i_snr );
363 msg_Dbg( p_access, "frontend has lost lock" );
364 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
369 /* The frontend was reinited. */
370 msg_Warn( p_access, "reiniting frontend");
371 FrontendSet( p_access );
377 int FrontendGetStatistic( access_t *p_access, frontend_statistic_t *p_stat )
379 access_sys_t *p_sys = p_access->p_sys;
380 frontend_t * p_frontend = p_sys->p_frontend;
382 if( (p_frontend->i_last_status & FE_HAS_LOCK) == 0 )
385 memset( p_stat, 0, sizeof(*p_stat) );
386 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &p_stat->i_ber ) < 0 )
388 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &p_stat->i_signal_strenth ) < 0 )
389 p_stat->i_signal_strenth = -1;
390 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &p_stat->i_snr ) < 0 )
395 void FrontendGetStatus( access_t *p_access, frontend_status_t *p_status )
397 access_sys_t *p_sys = p_access->p_sys;
398 frontend_t * p_frontend = p_sys->p_frontend;
400 p_status->b_has_signal = (p_frontend->i_last_status & FE_HAS_SIGNAL) != 0;
401 p_status->b_has_carrier = (p_frontend->i_last_status & FE_HAS_CARRIER) != 0;
402 p_status->b_has_lock = (p_frontend->i_last_status & FE_HAS_LOCK) != 0;
404 static int ScanParametersDvbC( access_t *p_access, scan_parameter_t *p_scan )
406 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
409 memset( p_scan, 0, sizeof(*p_scan) );
410 p_scan->type = SCAN_DVB_C;
411 p_scan->b_exhaustive = false;
414 p_scan->frequency.i_min = p_frontend->info.frequency_min;
415 p_scan->frequency.i_max = p_frontend->info.frequency_max;
416 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
417 ? p_frontend->info.frequency_stepsize : 166667;
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 static int ScanParametersDvbT( access_t *p_access, scan_parameter_t *p_scan )
429 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
432 memset( p_scan, 0, sizeof(*p_scan) );
433 p_scan->type = SCAN_DVB_T;
434 p_scan->b_exhaustive = false;
437 p_scan->frequency.i_min = p_frontend->info.frequency_min;
438 p_scan->frequency.i_max = p_frontend->info.frequency_max;
439 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
440 ? p_frontend->info.frequency_stepsize : 166667;
441 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
444 p_scan->bandwidth.i_min = 6;
445 p_scan->bandwidth.i_max = 8;
446 p_scan->bandwidth.i_step = 1;
447 p_scan->bandwidth.i_count = 3;
450 int FrontendGetScanParameter( access_t *p_access, scan_parameter_t *p_scan )
452 access_sys_t *p_sys = p_access->p_sys;
453 const frontend_t *p_frontend = p_sys->p_frontend;
455 if( p_frontend->info.type == FE_OFDM ) // DVB-T
456 return ScanParametersDvbT( p_access, p_scan );
457 else if( p_frontend->info.type == FE_QAM ) // DVB-C
458 return ScanParametersDvbC( p_access, p_scan );
460 msg_Err( p_access, "Frontend type not supported for scanning" );
465 /*****************************************************************************
466 * FrontendStatus : Read frontend status
467 *****************************************************************************/
468 void FrontendStatus( access_t *p_access )
470 access_sys_t *p_sys = p_access->p_sys;
471 frontend_t *p_frontend = p_sys->p_frontend;
472 char *p = p_sys->psz_frontend_info = malloc( 10000 );
473 fe_status_t i_status;
476 /* Determine type of frontend */
477 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
478 &p_frontend->info )) < 0 )
481 strerror_r( errno, buf, sizeof( buf ) );
482 p += sprintf( p, "ioctl FE_GET_INFO failed (%d) %s\n", i_ret, buf );
486 /* Print out frontend capabilities. */
487 p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
488 p_frontend->info.name );
489 switch( p_frontend->info.type )
492 p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
495 p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
498 p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
500 #if 0 /* DVB_API_VERSION == 3 */
502 p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
505 p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
509 p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
510 p_frontend->info.type );
513 #define CHECK_INFO( x ) \
515 "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n", \
516 p_frontend->info.x );
518 CHECK_INFO( frequency_min );
519 CHECK_INFO( frequency_max );
520 CHECK_INFO( frequency_stepsize );
521 CHECK_INFO( frequency_tolerance );
522 CHECK_INFO( symbol_rate_min );
523 CHECK_INFO( symbol_rate_max );
524 CHECK_INFO( symbol_rate_tolerance );
525 CHECK_INFO( notifier_delay );
528 p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
530 #define CHECK_CAPS( x ) \
531 if ( p_frontend->info.caps & (FE_##x) ) \
532 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
534 CHECK_CAPS( IS_STUPID );
535 CHECK_CAPS( CAN_INVERSION_AUTO );
536 CHECK_CAPS( CAN_FEC_1_2 );
537 CHECK_CAPS( CAN_FEC_2_3 );
538 CHECK_CAPS( CAN_FEC_3_4 );
539 CHECK_CAPS( CAN_FEC_4_5 );
540 CHECK_CAPS( CAN_FEC_5_6 );
541 CHECK_CAPS( CAN_FEC_6_7 );
542 CHECK_CAPS( CAN_FEC_7_8 );
543 CHECK_CAPS( CAN_FEC_8_9 );
544 CHECK_CAPS( CAN_FEC_AUTO );
545 CHECK_CAPS( CAN_QPSK );
546 CHECK_CAPS( CAN_QAM_16 );
547 CHECK_CAPS( CAN_QAM_32 );
548 CHECK_CAPS( CAN_QAM_64 );
549 CHECK_CAPS( CAN_QAM_128 );
550 CHECK_CAPS( CAN_QAM_256 );
551 CHECK_CAPS( CAN_QAM_AUTO );
552 CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
553 CHECK_CAPS( CAN_BANDWIDTH_AUTO );
554 CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
555 CHECK_CAPS( CAN_HIERARCHY_AUTO );
556 CHECK_CAPS( CAN_MUTE_TS );
557 CHECK_CAPS( CAN_RECOVER );
558 #if 0 /* Disabled because of older distributions */
559 CHECK_CAPS( CAN_CLEAN_SETUP );
563 p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
565 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ))
569 strerror_r( errno, buf, sizeof( buf ) );
570 p += sprintf( p, "</table>ioctl FE_READ_STATUS failed (%d) %s\n",
575 #define CHECK_STATUS( x ) \
576 if ( i_status & (FE_##x) ) \
577 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
579 CHECK_STATUS( HAS_SIGNAL );
580 CHECK_STATUS( HAS_CARRIER );
581 CHECK_STATUS( HAS_VITERBI );
582 CHECK_STATUS( HAS_SYNC );
583 CHECK_STATUS( HAS_LOCK );
584 CHECK_STATUS( REINIT );
586 p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
589 if ( i_status & FE_HAS_LOCK )
592 p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
593 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
594 p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
596 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
598 p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
600 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
601 p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
604 p += sprintf( p, "</table>" );
607 vlc_mutex_lock( &p_sys->httpd_mutex );
608 p_sys->b_request_frontend_info = false;
609 vlc_cond_signal( &p_sys->httpd_cond );
610 vlc_mutex_unlock( &p_sys->httpd_mutex );
614 /*****************************************************************************
615 * FrontendInfo : Return information about given frontend
616 *****************************************************************************/
617 static int FrontendInfo( access_t *p_access )
619 access_sys_t *p_sys = p_access->p_sys;
620 frontend_t *p_frontend = p_sys->p_frontend;
623 /* Determine type of frontend */
624 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
625 &p_frontend->info )) < 0 )
627 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d): %m", i_ret );
631 /* Print out frontend capabilities. */
632 msg_Dbg(p_access, "Frontend Info:" );
633 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
634 switch( p_frontend->info.type )
637 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
640 msg_Dbg( p_access, " type = QAM (DVB-C)" );
643 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
646 msg_Dbg( p_access, " type = ATSC (USA)" );
648 #if 0 /* DVB_API_VERSION == 3 */
650 msg_Dbg(p_access, " type = MEMORY" );
653 msg_Dbg(p_access, " type = NETWORK" );
657 msg_Err( p_access, " unknown frontend type (%d)",
658 p_frontend->info.type );
661 msg_Dbg(p_access, " frequency_min = %u (kHz)",
662 p_frontend->info.frequency_min);
663 msg_Dbg(p_access, " frequency_max = %u (kHz)",
664 p_frontend->info.frequency_max);
665 msg_Dbg(p_access, " frequency_stepsize = %u",
666 p_frontend->info.frequency_stepsize);
667 msg_Dbg(p_access, " frequency_tolerance = %u",
668 p_frontend->info.frequency_tolerance);
669 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
670 p_frontend->info.symbol_rate_min);
671 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
672 p_frontend->info.symbol_rate_max);
673 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
674 p_frontend->info.symbol_rate_tolerance);
675 msg_Dbg(p_access, " notifier_delay (ms) = %u",
676 p_frontend->info.notifier_delay );
678 msg_Dbg(p_access, "Frontend Info capability list:");
679 if( p_frontend->info.caps & FE_IS_STUPID)
680 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
681 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
682 msg_Dbg(p_access, " inversion auto");
683 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
684 msg_Dbg(p_access, " forward error correction 1/2");
685 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
686 msg_Dbg(p_access, " forward error correction 2/3");
687 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
688 msg_Dbg(p_access, " forward error correction 3/4");
689 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
690 msg_Dbg(p_access, " forward error correction 4/5");
691 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
692 msg_Dbg(p_access, " forward error correction 5/6");
693 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
694 msg_Dbg(p_access, " forward error correction 6/7");
695 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
696 msg_Dbg(p_access, " forward error correction 7/8");
697 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
698 msg_Dbg(p_access, " forward error correction 8/9");
699 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
700 msg_Dbg(p_access, " forward error correction auto");
701 if( p_frontend->info.caps & FE_CAN_QPSK)
702 msg_Dbg(p_access, " card can do QPSK");
703 if( p_frontend->info.caps & FE_CAN_QAM_16)
704 msg_Dbg(p_access, " card can do QAM 16");
705 if( p_frontend->info.caps & FE_CAN_QAM_32)
706 msg_Dbg(p_access, " card can do QAM 32");
707 if( p_frontend->info.caps & FE_CAN_QAM_64)
708 msg_Dbg(p_access, " card can do QAM 64");
709 if( p_frontend->info.caps & FE_CAN_QAM_128)
710 msg_Dbg(p_access, " card can do QAM 128");
711 if( p_frontend->info.caps & FE_CAN_QAM_256)
712 msg_Dbg(p_access, " card can do QAM 256");
713 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
714 msg_Dbg(p_access, " card can do QAM auto");
715 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
716 msg_Dbg(p_access, " transmission mode auto");
717 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
718 msg_Dbg(p_access, " bandwidth mode auto");
719 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
720 msg_Dbg(p_access, " guard interval mode auto");
721 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
722 msg_Dbg(p_access, " hierarchy mode auto");
723 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
724 msg_Dbg(p_access, " card can mute TS");
725 if( p_frontend->info.caps & FE_CAN_RECOVER)
726 msg_Dbg(p_access, " card can recover from a cable unplug");
727 if( p_frontend->info.caps & FE_CAN_8VSB)
728 msg_Dbg(p_access, " card can do 8vsb");
729 if( p_frontend->info.caps & FE_CAN_16VSB)
730 msg_Dbg(p_access, " card can do 16vsb");
731 msg_Dbg(p_access, "End of capability list");
736 /*****************************************************************************
737 * Decoding the DVB parameters (common)
738 *****************************************************************************/
739 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
742 fe_spectral_inversion_t fe_inversion = 0;
744 i_val = var_GetInteger( p_access, "dvb-inversion" );
745 msg_Dbg( p_access, "using inversion=%d", i_val );
749 case 0: fe_inversion = INVERSION_OFF; break;
750 case 1: fe_inversion = INVERSION_ON; break;
751 case 2: fe_inversion = INVERSION_AUTO; break;
753 msg_Dbg( p_access, "dvb has inversion not set, using auto");
754 fe_inversion = INVERSION_AUTO;
760 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
762 fe_code_rate_t fe_fec = FEC_NONE;
764 msg_Dbg( p_access, "using fec=%d", i_val );
768 case 0: fe_fec = FEC_NONE; break;
769 case 1: fe_fec = FEC_1_2; break;
770 case 2: fe_fec = FEC_2_3; break;
771 case 3: fe_fec = FEC_3_4; break;
772 case 4: fe_fec = FEC_4_5; break;
773 case 5: fe_fec = FEC_5_6; break;
774 case 6: fe_fec = FEC_6_7; break;
775 case 7: fe_fec = FEC_7_8; break;
776 case 8: fe_fec = FEC_8_9; break;
777 case 9: fe_fec = FEC_AUTO; break;
781 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
787 static fe_modulation_t DecodeModulationQAM( access_t *p_access )
789 switch( var_GetInteger( p_access, "dvb-modulation" ) )
791 case 0: return QAM_AUTO;
792 case 16: return QAM_16;
793 case 32: return QAM_32;
794 case 64: return QAM_64;
795 case 128: return QAM_128;
796 case 256: return QAM_256;
798 msg_Dbg( p_access, "QAM modulation not set, using auto");
802 static fe_modulation_t DecodeModulationOFDM( access_t *p_access )
804 switch( var_GetInteger( p_access, "dvb-modulation" ) )
806 case -1: return QPSK;
807 case 0: return QAM_AUTO;
808 case 16: return QAM_16;
809 case 32: return QAM_32;
810 case 64: return QAM_64;
811 case 128: return QAM_128;
812 case 256: return QAM_256;
814 msg_Dbg( p_access, "OFDM modulation not set, using QAM auto");
818 static fe_modulation_t DecodeModulationATSC( access_t *p_access )
820 switch( var_GetInteger( p_access, "dvb-modulation" ) )
822 case 0: return QAM_AUTO;
823 case 8: return VSB_8;
824 case 16: return VSB_16;
825 case 32: return QAM_32;
826 case 64: return QAM_64;
827 case 128: return QAM_128;
828 case 256: return QAM_256;
830 msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
835 /*****************************************************************************
836 * FrontendSetQPSK : controls the FE device
837 *****************************************************************************/
838 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
841 fe_sec_voltage_t fe_voltage;
843 i_val = var_GetInteger( p_access, "dvb-voltage" );
844 msg_Dbg( p_access, "using voltage=%d", i_val );
848 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
849 case 13: fe_voltage = SEC_VOLTAGE_13; break;
850 case 18: fe_voltage = SEC_VOLTAGE_18; break;
852 fe_voltage = SEC_VOLTAGE_OFF;
853 msg_Err( p_access, "argument has invalid voltage (%d)", i_val );
859 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
862 fe_sec_tone_mode_t fe_tone;
864 i_val = var_GetInteger( p_access, "dvb-tone" );
865 msg_Dbg( p_access, "using tone=%d", i_val );
869 case 0: fe_tone = SEC_TONE_OFF; break;
870 case 1: fe_tone = SEC_TONE_ON; break;
872 fe_tone = SEC_TONE_OFF;
873 msg_Err( p_access, "argument has invalid tone mode (%d)", i_val );
881 struct dvb_diseqc_master_cmd cmd;
885 static int DoDiseqc( access_t *p_access )
887 access_sys_t *p_sys = p_access->p_sys;
890 int i_frequency, i_lnb_slof;
891 fe_sec_voltage_t fe_voltage;
892 fe_sec_tone_mode_t fe_tone;
895 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
896 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
898 i_val = var_GetInteger( p_access, "dvb-tone" );
899 if( i_val == -1 /* auto */ )
901 if( i_frequency >= i_lnb_slof )
905 var_SetInteger( p_access, "dvb-tone", i_val );
908 fe_voltage = DecodeVoltage( p_access );
909 fe_tone = DecodeTone( p_access );
911 /* Switch off continuous tone. */
912 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
914 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
915 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
919 /* Configure LNB voltage. */
920 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
922 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
927 b_val = var_GetBool( p_access, "dvb-high-voltage" );
928 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
929 b_val )) < 0 && b_val )
932 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
936 /* Wait for at least 15 ms. */
939 i_val = var_GetInteger( p_access, "dvb-satno" );
940 if( i_val > 0 && i_val < 5 )
942 /* digital satellite equipment control,
943 * specification is available from http://www.eutelsat.com/
946 /* 1.x compatible equipment */
947 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
949 /* param: high nibble: reset bits, low nibble set bits,
950 * bits are: option, position, polarization, band
952 cmd.cmd.msg[3] = 0xf0 /* reset bits */
953 | (((i_val - 1) * 4) & 0xc)
954 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
955 | (fe_tone == SEC_TONE_ON ? 1 : 0);
957 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
960 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
965 msleep(15000 + cmd.wait * 1000);
967 /* A or B simple diseqc ("diseqc-compatible") */
968 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
969 ((i_val - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
971 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
979 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
981 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
982 fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
990 static int FrontendSetQPSK( access_t *p_access )
992 access_sys_t *p_sys = p_access->p_sys;
993 struct dvb_frontend_parameters fep;
996 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
998 /* Prepare the fep structure */
999 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
1001 i_val = var_GetInteger( p_access, "dvb-lnb-lof1" );
1004 /* Automatic mode. */
1005 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
1007 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
1010 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
1012 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
1013 i_lnb_lof1 = 3650000;
1015 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
1017 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
1019 i_lnb_lof1 = 5150000;
1021 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
1023 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
1025 i_lnb_lof1 = 5950000;
1027 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1029 msg_Dbg( p_access, "frequency %d is in Ku-band",
1031 i_lnb_lof1 = 9750000;
1032 i_lnb_lof2 = 10600000;
1033 i_lnb_slof = 11700000;
1037 msg_Err( p_access, "frequency %d is out of any known band",
1039 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1040 "oscillator frequency" );
1041 return VLC_EGENERIC;
1043 var_SetInteger( p_access, "dvb-lnb-lof1", i_lnb_lof1 );
1044 var_SetInteger( p_access, "dvb-lnb-lof2", i_lnb_lof2 );
1045 var_SetInteger( p_access, "dvb-lnb-slof", i_lnb_slof );
1050 i_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
1051 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
1054 if( i_lnb_slof && i_frequency >= i_lnb_slof )
1056 i_frequency -= i_lnb_lof2;
1060 i_frequency -= i_lnb_lof1;
1062 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1064 fep.inversion = DecodeInversion( p_access );
1066 fep.u.qpsk.symbol_rate = var_GetInteger( p_access, "dvb-srate" );
1068 fep.u.qpsk.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access, "dvb-fec" ) );
1070 if( DoDiseqc( p_access ) < 0 )
1072 return VLC_EGENERIC;
1075 /* Empty the event queue */
1078 struct dvb_frontend_event event;
1079 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1080 && errno == EWOULDBLOCK )
1084 /* Now send it all to the frontend device */
1085 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1087 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1088 return VLC_EGENERIC;
1094 /*****************************************************************************
1095 * FrontendSetQAM : controls the FE device
1096 *****************************************************************************/
1097 static int FrontendSetQAM( access_t *p_access )
1099 access_sys_t *p_sys = p_access->p_sys;
1100 frontend_t *p_frontend = p_sys->p_frontend;
1101 struct dvb_frontend_parameters fep;
1105 /* Prepare the fep structure */
1107 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1109 fep.inversion = DecodeInversion( p_access );
1111 /* Default symbol-rate is for dvb-s, and doesn't fit
1112 * for dvb-c, so if it's over the limit of frontend, default to
1113 * somewhat common value
1115 i_val = var_GetInteger( p_access, "dvb-srate" );
1116 if( i_val < p_frontend->info.symbol_rate_max &&
1117 i_val > p_frontend->info.symbol_rate_min )
1118 fep.u.qam.symbol_rate = i_val;
1120 fep.u.qam.symbol_rate = 6875000;
1122 fep.u.qam.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access,
1125 fep.u.qam.modulation = DecodeModulationQAM( p_access );
1127 /* Empty the event queue */
1130 struct dvb_frontend_event event;
1131 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1132 && errno == EWOULDBLOCK )
1136 /* Now send it all to the frontend device */
1137 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1139 msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1140 return VLC_EGENERIC;
1146 /*****************************************************************************
1147 * FrontendSetOFDM : controls the FE device
1148 *****************************************************************************/
1149 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1151 fe_bandwidth_t fe_bandwidth = 0;
1152 int i_bandwith = var_GetInteger( p_access, "dvb-bandwidth" );
1154 msg_Dbg( p_access, "using bandwidth=%d", i_bandwith );
1156 switch( i_bandwith )
1158 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1159 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1160 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1161 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1163 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1164 fe_bandwidth = BANDWIDTH_AUTO;
1167 return fe_bandwidth;
1170 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1172 fe_transmit_mode_t fe_transmission = 0;
1173 int i_transmission = var_GetInteger( p_access, "dvb-transmission" );
1175 msg_Dbg( p_access, "using transmission=%d", i_transmission );
1177 switch( i_transmission )
1179 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1180 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1181 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1183 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1184 fe_transmission = TRANSMISSION_MODE_AUTO;
1187 return fe_transmission;
1190 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1192 fe_guard_interval_t fe_guard = 0;
1193 int i_guard = var_GetInteger( p_access, "dvb-guard" );
1195 msg_Dbg( p_access, "using guard=%d", i_guard );
1199 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1200 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1201 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1202 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1203 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1205 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1206 fe_guard = GUARD_INTERVAL_AUTO;
1212 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1214 fe_hierarchy_t fe_hierarchy = 0;
1215 int i_hierarchy = var_GetInteger( p_access, "dvb-hierarchy" );
1217 msg_Dbg( p_access, "using hierarchy=%d", i_hierarchy );
1219 switch( i_hierarchy )
1221 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1222 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1223 case 1: fe_hierarchy = HIERARCHY_1; break;
1224 case 2: fe_hierarchy = HIERARCHY_2; break;
1225 case 4: fe_hierarchy = HIERARCHY_4; break;
1227 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1228 fe_hierarchy = HIERARCHY_AUTO;
1231 return fe_hierarchy;
1234 static int FrontendSetOFDM( access_t * p_access )
1236 access_sys_t *p_sys = p_access->p_sys;
1237 struct dvb_frontend_parameters fep;
1240 /* Prepare the fep structure */
1242 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1244 fep.inversion = DecodeInversion( p_access );
1246 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1247 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, var_GetInteger( p_access,
1248 "dvb-code-rate-hp" ) );
1249 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, var_GetInteger( p_access,
1250 "dvb-code-rate-lp" ) );
1251 fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1252 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1253 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1254 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1256 /* Empty the event queue */
1259 struct dvb_frontend_event event;
1260 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1261 && errno == EWOULDBLOCK )
1265 /* Now send it all to the frontend device */
1266 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1268 msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1275 /*****************************************************************************
1276 * FrontendSetATSC : controls the FE device
1277 *****************************************************************************/
1278 static int FrontendSetATSC( access_t *p_access )
1280 access_sys_t *p_sys = p_access->p_sys;
1281 struct dvb_frontend_parameters fep;
1284 /* Prepare the fep structure */
1286 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1288 fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1290 /* Empty the event queue */
1293 struct dvb_frontend_event event;
1294 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1295 && errno == EWOULDBLOCK )
1299 /* Now send it all to the frontend device */
1300 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1302 msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1303 return VLC_EGENERIC;
1314 /*****************************************************************************
1315 * DMXSetFilter : controls the demux to add a filter
1316 *****************************************************************************/
1317 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1319 struct dmx_pes_filter_params s_filter_params;
1321 unsigned int i_adapter, i_device;
1324 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1325 i_device = var_GetInteger( p_access, "dvb-device" );
1327 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1328 >= (int)sizeof(dmx) )
1330 msg_Err( p_access, "snprintf() truncated string for DMX" );
1331 dmx[sizeof(dmx) - 1] = '\0';
1334 msg_Dbg( p_access, "Opening device %s", dmx );
1335 if( (*pi_fd = vlc_open(dmx, O_RDWR)) < 0 )
1337 msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1338 return VLC_EGENERIC;
1341 /* We fill the DEMUX structure : */
1342 s_filter_params.pid = i_pid;
1343 s_filter_params.input = DMX_IN_FRONTEND;
1344 s_filter_params.output = DMX_OUT_TS_TAP;
1345 s_filter_params.flags = DMX_IMMEDIATE_START;
1348 { /* First device */
1350 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1351 s_filter_params.pes_type = DMX_PES_VIDEO0;
1354 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1355 s_filter_params.pes_type = DMX_PES_AUDIO0;
1358 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1359 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1362 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1363 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1366 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1367 s_filter_params.pes_type = DMX_PES_PCR0;
1371 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1372 s_filter_params.pes_type = DMX_PES_VIDEO1;
1375 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1376 s_filter_params.pes_type = DMX_PES_AUDIO1;
1379 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1380 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1383 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1384 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1387 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1388 s_filter_params.pes_type = DMX_PES_PCR1;
1392 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1393 s_filter_params.pes_type = DMX_PES_VIDEO2;
1396 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1397 s_filter_params.pes_type = DMX_PES_AUDIO2;
1400 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1401 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1404 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1405 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1408 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1409 s_filter_params.pes_type = DMX_PES_PCR2;
1413 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1414 s_filter_params.pes_type = DMX_PES_VIDEO3;
1417 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1418 s_filter_params.pes_type = DMX_PES_AUDIO3;
1421 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1422 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1425 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1426 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1429 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1430 s_filter_params.pes_type = DMX_PES_PCR3;
1432 /* Usually used by Nova cards */
1435 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1436 s_filter_params.pes_type = DMX_PES_OTHER;
1440 /* We then give the order to the device : */
1441 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1443 msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1444 return VLC_EGENERIC;
1449 /*****************************************************************************
1450 * DMXUnsetFilter : removes a filter
1451 *****************************************************************************/
1452 int DMXUnsetFilter( access_t * p_access, int i_fd )
1456 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1458 msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1462 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1472 /*****************************************************************************
1474 *****************************************************************************/
1475 int DVROpen( access_t * p_access )
1477 access_sys_t *p_sys = p_access->p_sys;
1478 unsigned int i_adapter, i_device;
1481 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1482 i_device = var_GetInteger( p_access, "dvb-device" );
1484 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1485 >= (int)sizeof(dvr) )
1487 msg_Err( p_access, "snprintf() truncated string for DVR" );
1488 dvr[sizeof(dvr) - 1] = '\0';
1491 msg_Dbg( p_access, "Opening device %s", dvr );
1492 if( (p_sys->i_handle = vlc_open(dvr, O_RDONLY)) < 0 )
1494 msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1495 return VLC_EGENERIC;
1498 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1500 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1506 /*****************************************************************************
1508 *****************************************************************************/
1509 void DVRClose( access_t * p_access )
1511 access_sys_t *p_sys = p_access->p_sys;
1513 close( p_sys->i_handle );
1521 /*****************************************************************************
1523 *****************************************************************************/
1524 int CAMOpen( access_t *p_access )
1526 access_sys_t *p_sys = p_access->p_sys;
1528 int i_adapter, i_device;
1531 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1532 i_device = var_GetInteger( p_access, "dvb-device" );
1534 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1536 msg_Err( p_access, "snprintf() truncated string for CA" );
1537 ca[sizeof(ca) - 1] = '\0';
1539 memset( &caps, 0, sizeof( ca_caps_t ));
1541 msg_Dbg( p_access, "Opening device %s", ca );
1542 if( (p_sys->i_ca_handle = vlc_open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1544 msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1545 p_sys->i_ca_handle = 0;
1546 return VLC_EGENERIC;
1549 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1551 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1552 close( p_sys->i_ca_handle );
1553 p_sys->i_ca_handle = 0;
1554 return VLC_EGENERIC;
1557 /* Output CA capabilities */
1558 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1559 caps.slot_num == 1 ? "slot" : "slots" );
1560 if ( caps.slot_type & CA_CI )
1561 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1562 if ( caps.slot_type & CA_CI_LINK )
1563 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1564 if ( caps.slot_type & CA_CI_PHYS )
1565 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1566 if ( caps.slot_type & CA_DESCR )
1567 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1568 if ( caps.slot_type & CA_SC )
1569 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1571 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1572 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1573 if ( caps.descr_type & CA_ECD )
1574 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1575 if ( caps.descr_type & CA_NDS )
1576 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1577 if ( caps.descr_type & CA_DSS )
1578 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1580 if ( caps.slot_num == 0 )
1582 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1583 close( p_sys->i_ca_handle );
1584 p_sys->i_ca_handle = 0;
1585 return VLC_EGENERIC;
1588 if( caps.slot_type & CA_CI_LINK )
1590 p_sys->i_ca_type = CA_CI_LINK;
1592 else if( caps.slot_type & CA_CI )
1594 p_sys->i_ca_type = CA_CI;
1598 p_sys->i_ca_type = -1;
1599 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1600 close( p_sys->i_ca_handle );
1601 p_sys->i_ca_handle = 0;
1602 return VLC_EGENERIC;
1605 p_sys->i_nb_slots = caps.slot_num;
1606 memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1607 memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1608 memset( p_sys->pb_slot_mmi_undisplayed, 0,
1609 sizeof(bool) * MAX_CI_SLOTS );
1611 return en50221_Init( p_access );
1614 /*****************************************************************************
1616 *****************************************************************************/
1617 int CAMPoll( access_t * p_access )
1619 access_sys_t *p_sys = p_access->p_sys;
1620 int i_ret = VLC_EGENERIC;
1622 if ( p_sys->i_ca_handle == 0 )
1624 return VLC_EGENERIC;
1627 switch( p_sys->i_ca_type )
1630 i_ret = en50221_Poll( p_access );
1633 i_ret = VLC_SUCCESS;
1636 msg_Err( p_access, "CAMPoll: This should not happen" );
1644 /*****************************************************************************
1646 *****************************************************************************/
1647 void CAMStatus( access_t * p_access )
1649 access_sys_t *p_sys = p_access->p_sys;
1654 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1656 /* Check if we have an undisplayed MMI message : in that case we ignore
1657 * the user input to avoid confusing the CAM. */
1658 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1660 if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1662 p_sys->psz_request = NULL;
1664 "ignoring user request because of a new MMI object" );
1670 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1672 /* We have a mission to accomplish. */
1673 en50221_mmi_object_t mmi_object;
1674 char *psz_request = p_sys->psz_request;
1675 char psz_value[255];
1679 p_sys->psz_request = NULL;
1681 if ( HTTPExtractValue( psz_request, "slot", psz_value,
1682 sizeof(psz_value) ) == NULL )
1684 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1687 i_slot = atoi(psz_value);
1689 if ( HTTPExtractValue( psz_request, "open", psz_value,
1690 sizeof(psz_value) ) != NULL )
1692 en50221_OpenMMI( p_access, i_slot );
1696 if ( HTTPExtractValue( psz_request, "close", psz_value,
1697 sizeof(psz_value) ) != NULL )
1699 en50221_CloseMMI( p_access, i_slot );
1703 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1704 sizeof(psz_value) ) == NULL )
1709 if ( HTTPExtractValue( psz_request, "type", psz_value,
1710 sizeof(psz_value) ) == NULL )
1712 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1716 if ( !strcmp( psz_value, "enq" ) )
1718 mmi_object.i_object_type = EN50221_MMI_ANSW;
1719 mmi_object.u.answ.b_ok = b_ok;
1720 if ( b_ok == false )
1722 mmi_object.u.answ.psz_answ = strdup("");
1726 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1727 sizeof(psz_value) ) == NULL )
1729 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1733 mmi_object.u.answ.psz_answ = strdup(psz_value);
1738 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1739 if ( b_ok == false )
1741 mmi_object.u.menu_answ.i_choice = 0;
1745 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1746 sizeof(psz_value) ) == NULL )
1747 mmi_object.u.menu_answ.i_choice = 0;
1749 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1753 en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1757 /* Check that we have all necessary MMI information. */
1758 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1760 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1764 p = p_sys->psz_mmi_info = malloc( 10000 );
1766 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1769 strerror_r( errno, buf, sizeof( buf ) );
1770 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1774 /* Output CA capabilities */
1775 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1776 caps.slot_num == 1 ? "slot" : "slots" );
1777 #define CHECK_CAPS( x, s ) \
1778 if ( caps.slot_type & (CA_##x) ) \
1779 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1781 CHECK_CAPS( CI, "CI high level interface" );
1782 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1783 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1784 CHECK_CAPS( DESCR, "built-in descrambler" );
1785 CHECK_CAPS( SC, "simple smartcard interface" );
1788 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1789 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1790 #define CHECK_DESC( x ) \
1791 if ( caps.descr_type & (CA_##x) ) \
1792 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1799 p += sprintf( p, "</table>" );
1801 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1803 ca_slot_info_t sinfo;
1805 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1806 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1809 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1812 strerror_r( errno, buf, sizeof( buf ) );
1813 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1817 #define CHECK_TYPE( x, s ) \
1818 if ( sinfo.type & (CA_##x) ) \
1819 p += sprintf( p, "%s", s );
1821 CHECK_TYPE( CI, "high level, " );
1822 CHECK_TYPE( CI_LINK, "link layer level, " );
1823 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1826 if ( sinfo.flags & CA_CI_MODULE_READY )
1828 en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1831 p += sprintf( p, "module present and ready<p>\n" );
1832 p += sprintf( p, "<form action=index.html method=get>\n" );
1833 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1836 if ( p_object == NULL )
1838 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1842 switch ( p_object->i_object_type )
1844 case EN50221_MMI_ENQ:
1845 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1846 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1847 p_object->u.enq.psz_text );
1848 if ( p_object->u.enq.b_blind == false )
1849 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1851 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1854 case EN50221_MMI_MENU:
1855 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1856 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1857 p_object->u.menu.psz_title );
1858 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1859 p_object->u.menu.psz_subtitle );
1860 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1861 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1862 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1863 p_object->u.menu.psz_bottom );
1866 case EN50221_MMI_LIST:
1867 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1868 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1869 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1870 p_object->u.menu.psz_title );
1871 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1872 p_object->u.menu.psz_subtitle );
1873 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1874 p += sprintf( p, "%s<br>\n",
1875 p_object->u.menu.ppsz_choices[i] );
1876 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1877 p_object->u.menu.psz_bottom );
1881 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1884 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1885 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1886 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1888 p += sprintf( p, "</form>\n" );
1890 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1891 p += sprintf( p, "module present, not ready<br>\n" );
1893 p += sprintf( p, "module not present<br>\n" );
1897 vlc_mutex_lock( &p_sys->httpd_mutex );
1898 p_sys->b_request_mmi_info = false;
1899 vlc_cond_signal( &p_sys->httpd_cond );
1900 vlc_mutex_unlock( &p_sys->httpd_mutex );
1904 /*****************************************************************************
1906 *****************************************************************************/
1907 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1909 access_sys_t *p_sys = p_access->p_sys;
1911 if( p_sys->i_ca_handle == 0 )
1913 dvbpsi_DeletePMT( p_pmt );
1914 return VLC_EGENERIC;
1917 en50221_SetCAPMT( p_access, p_pmt );
1922 /*****************************************************************************
1924 *****************************************************************************/
1925 void CAMClose( access_t * p_access )
1927 access_sys_t *p_sys = p_access->p_sys;
1929 en50221_End( p_access );
1931 if ( p_sys->i_ca_handle )
1933 close( p_sys->i_ca_handle );