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@wxs.nl>
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 *****************************************************************************/
28 #include <vlc/input.h>
30 #include <sys/ioctl.h>
33 #include <sys/types.h>
41 /* DVB Card Drivers */
42 #include <linux/dvb/version.h>
43 #include <linux/dvb/dmx.h>
44 #include <linux/dvb/frontend.h>
45 #include <linux/dvb/ca.h>
47 /* Include dvbpsi headers */
48 #ifdef HAVE_DVBPSI_DR_H
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/descriptor.h>
51 # include <dvbpsi/pat.h>
52 # include <dvbpsi/pmt.h>
53 # include <dvbpsi/dr.h>
54 # include <dvbpsi/psi.h>
57 # include "descriptor.h"
58 # include "tables/pat.h"
59 # include "tables/pmt.h"
60 # include "descriptors/dr.h"
65 # include "vlc_httpd.h"
75 fe_status_t i_last_status;
76 struct dvb_frontend_info info;
79 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
81 /* Local prototypes */
82 static int FrontendInfo( access_t * );
83 static int FrontendSetQPSK( access_t * );
84 static int FrontendSetQAM( access_t * );
85 static int FrontendSetOFDM( access_t * );
87 /*****************************************************************************
88 * FrontendOpen : Determine frontend device information and capabilities
89 *****************************************************************************/
90 int E_(FrontendOpen)( access_t *p_access )
92 access_sys_t *p_sys = p_access->p_sys;
93 frontend_t * p_frontend;
94 unsigned int i_adapter, i_device;
98 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
99 i_device = var_GetInteger( p_access, "dvb-device" );
100 b_probe = var_GetBool( p_access, "dvb-probe" );
102 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
104 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
105 frontend[sizeof(frontend) - 1] = '\0';
108 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
110 msg_Dbg( p_access, "Opening device %s", frontend );
111 if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
113 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
121 char * psz_expected = NULL;
124 if( FrontendInfo( p_access ) < 0 )
126 close( p_sys->i_frontend_handle );
131 switch( p_frontend->info.type )
143 psz_real = "unknown";
147 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
148 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
149 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
150 (p_frontend->info.type != FE_QPSK) )
152 psz_expected = "DVB-S";
154 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
155 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
156 (p_frontend->info.type != FE_QAM) )
158 psz_expected = "DVB-C";
160 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
161 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
162 (p_frontend->info.type != FE_OFDM) )
164 psz_expected = "DVB-T";
167 if( psz_expected != NULL )
169 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
170 psz_expected, psz_real );
171 close( p_sys->i_frontend_handle );
176 else /* no frontend probing is done so use default border values. */
178 msg_Dbg( p_access, "using default values for frontend info" );
180 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
181 p_frontend->info.type = FE_QPSK;
182 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
183 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
184 p_frontend->info.type = FE_QPSK;
185 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
186 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
187 p_frontend->info.type = FE_QAM;
188 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
189 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
190 p_frontend->info.type = FE_OFDM;
196 /*****************************************************************************
197 * FrontendClose : Close the frontend
198 *****************************************************************************/
199 void E_(FrontendClose)( access_t *p_access )
201 access_sys_t *p_sys = p_access->p_sys;
203 if( p_sys->p_frontend )
205 close( p_sys->i_frontend_handle );
206 free( p_sys->p_frontend );
208 p_sys->p_frontend = NULL;
212 /*****************************************************************************
213 * FrontendSet : Tune !
214 *****************************************************************************/
215 int E_(FrontendSet)( access_t *p_access )
217 access_sys_t *p_sys = p_access->p_sys;
219 switch( p_sys->p_frontend->info.type )
223 if( FrontendSetQPSK( p_access ) < 0 )
225 msg_Err( p_access, "DVB-S: tuning failed" );
232 if( FrontendSetQAM( p_access ) < 0 )
234 msg_Err( p_access, "DVB-C: tuning failed" );
241 if( FrontendSetOFDM( p_access ) < 0 )
243 msg_Err( p_access, "DVB-T: tuning failed" );
249 msg_Err( p_access, "Could not determine frontend type on %s",
250 p_sys->p_frontend->info.name );
253 p_sys->p_frontend->i_last_status = 0;
254 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
258 /*****************************************************************************
259 * FrontendPoll : Poll for frontend events
260 *****************************************************************************/
261 void E_(FrontendPoll)( access_t *p_access )
263 access_sys_t *p_sys = p_access->p_sys;
264 frontend_t * p_frontend = p_sys->p_frontend;
265 struct dvb_frontend_event event;
266 fe_status_t i_status, i_diff;
270 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
274 if( errno == EWOULDBLOCK )
275 return; /* no more events */
277 msg_Err( p_access, "reading frontend event failed (%d) %s",
278 i_ret, strerror(errno) );
282 i_status = event.status;
283 i_diff = i_status ^ p_frontend->i_last_status;
284 p_frontend->i_last_status = i_status;
289 if ( i_diff & (x) ) \
291 if ( i_status & (x) )
293 IF_UP( FE_HAS_SIGNAL )
294 msg_Dbg( p_access, "frontend has acquired signal" );
296 msg_Dbg( p_access, "frontend has lost signal" );
298 IF_UP( FE_HAS_CARRIER )
299 msg_Dbg( p_access, "frontend has acquired carrier" );
301 msg_Dbg( p_access, "frontend has lost carrier" );
303 IF_UP( FE_HAS_VITERBI )
304 msg_Dbg( p_access, "frontend has acquired stable FEC" );
306 msg_Dbg( p_access, "frontend has lost FEC" );
309 msg_Dbg( p_access, "frontend has acquired sync" );
311 msg_Dbg( p_access, "frontend has lost sync" );
316 msg_Dbg( p_access, "frontend has acquired lock" );
317 p_sys->i_frontend_timeout = 0;
319 /* Read some statistics */
320 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
321 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
322 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
323 msg_Dbg( p_access, "- Signal strength: %d", i_value );
324 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
325 msg_Dbg( p_access, "- SNR: %d", i_value );
329 msg_Dbg( p_access, "frontend has lost lock" );
330 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
335 /* The frontend was reinited. */
336 msg_Warn( p_access, "reiniting frontend");
337 E_(FrontendSet)( p_access );
345 /*****************************************************************************
346 * FrontendStatus : Read frontend status
347 *****************************************************************************/
348 void E_(FrontendStatus)( access_t *p_access )
350 access_sys_t *p_sys = p_access->p_sys;
351 frontend_t *p_frontend = p_sys->p_frontend;
352 char *p = p_sys->psz_frontend_info = malloc( 10000 );
353 fe_status_t i_status;
356 /* Determine type of frontend */
357 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
358 &p_frontend->info )) < 0 )
360 p += sprintf( p, "ioctl FE_GET_INFO failed (%d) %s\n", i_ret,
365 /* Print out frontend capabilities. */
366 p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
367 p_frontend->info.name );
368 switch( p_frontend->info.type )
371 p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
374 p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
377 p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
379 #if 0 /* DVB_API_VERSION == 3 */
381 p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
384 p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
388 p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
389 p_frontend->info.type );
392 #define CHECK_INFO( x ) \
394 "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n", \
395 p_frontend->info.x );
397 CHECK_INFO( frequency_min );
398 CHECK_INFO( frequency_max );
399 CHECK_INFO( frequency_stepsize );
400 CHECK_INFO( frequency_tolerance );
401 CHECK_INFO( symbol_rate_min );
402 CHECK_INFO( symbol_rate_max );
403 CHECK_INFO( symbol_rate_tolerance );
404 CHECK_INFO( notifier_delay );
407 p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
409 #define CHECK_CAPS( x ) \
410 if ( p_frontend->info.caps & (FE_##x) ) \
411 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
413 CHECK_CAPS( IS_STUPID );
414 CHECK_CAPS( CAN_INVERSION_AUTO );
415 CHECK_CAPS( CAN_FEC_1_2 );
416 CHECK_CAPS( CAN_FEC_2_3 );
417 CHECK_CAPS( CAN_FEC_3_4 );
418 CHECK_CAPS( CAN_FEC_4_5 );
419 CHECK_CAPS( CAN_FEC_5_6 );
420 CHECK_CAPS( CAN_FEC_6_7 );
421 CHECK_CAPS( CAN_FEC_7_8 );
422 CHECK_CAPS( CAN_FEC_8_9 );
423 CHECK_CAPS( CAN_FEC_AUTO );
424 CHECK_CAPS( CAN_QPSK );
425 CHECK_CAPS( CAN_QAM_16 );
426 CHECK_CAPS( CAN_QAM_32 );
427 CHECK_CAPS( CAN_QAM_64 );
428 CHECK_CAPS( CAN_QAM_128 );
429 CHECK_CAPS( CAN_QAM_256 );
430 CHECK_CAPS( CAN_QAM_AUTO );
431 CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
432 CHECK_CAPS( CAN_BANDWIDTH_AUTO );
433 CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
434 CHECK_CAPS( CAN_HIERARCHY_AUTO );
435 CHECK_CAPS( CAN_MUTE_TS );
436 CHECK_CAPS( CAN_RECOVER );
437 #if 0 /* Disabled because of older distributions */
438 CHECK_CAPS( CAN_CLEAN_SETUP );
442 p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
444 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ))
447 p += sprintf( p, "</table>ioctl FE_READ_STATUS failed (%d) %s\n", i_ret,
452 #define CHECK_STATUS( x ) \
453 if ( i_status & (FE_##x) ) \
454 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
456 CHECK_STATUS( HAS_SIGNAL );
457 CHECK_STATUS( HAS_CARRIER );
458 CHECK_STATUS( HAS_VITERBI );
459 CHECK_STATUS( HAS_SYNC );
460 CHECK_STATUS( HAS_LOCK );
461 CHECK_STATUS( REINIT );
463 p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
466 if ( i_status & FE_HAS_LOCK )
469 p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
470 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
471 p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
473 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
475 p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
477 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
478 p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
481 p += sprintf( p, "</table>" );
484 vlc_mutex_lock( &p_sys->httpd_mutex );
485 p_sys->b_request_frontend_info = VLC_FALSE;
486 vlc_cond_signal( &p_sys->httpd_cond );
487 vlc_mutex_unlock( &p_sys->httpd_mutex );
491 /*****************************************************************************
492 * FrontendInfo : Return information about given frontend
493 *****************************************************************************/
494 static int FrontendInfo( access_t *p_access )
496 access_sys_t *p_sys = p_access->p_sys;
497 frontend_t *p_frontend = p_sys->p_frontend;
500 /* Determine type of frontend */
501 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
502 &p_frontend->info )) < 0 )
504 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
509 /* Print out frontend capabilities. */
510 msg_Dbg(p_access, "Frontend Info:" );
511 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
512 switch( p_frontend->info.type )
515 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
518 msg_Dbg( p_access, " type = QAM (DVB-C)" );
521 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
523 #if 0 /* DVB_API_VERSION == 3 */
525 msg_Dbg(p_access, " type = MEMORY" );
528 msg_Dbg(p_access, " type = NETWORK" );
532 msg_Err( p_access, " unknown frontend type (%d)",
533 p_frontend->info.type );
536 msg_Dbg(p_access, " frequency_min = %u (kHz)",
537 p_frontend->info.frequency_min);
538 msg_Dbg(p_access, " frequency_max = %u (kHz)",
539 p_frontend->info.frequency_max);
540 msg_Dbg(p_access, " frequency_stepsize = %u",
541 p_frontend->info.frequency_stepsize);
542 msg_Dbg(p_access, " frequency_tolerance = %u",
543 p_frontend->info.frequency_tolerance);
544 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
545 p_frontend->info.symbol_rate_min);
546 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
547 p_frontend->info.symbol_rate_max);
548 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
549 p_frontend->info.symbol_rate_tolerance);
550 msg_Dbg(p_access, " notifier_delay (ms) = %u",
551 p_frontend->info.notifier_delay );
553 msg_Dbg(p_access, "Frontend Info capability list:");
554 if( p_frontend->info.caps & FE_IS_STUPID)
555 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
556 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
557 msg_Dbg(p_access, " inversion auto");
558 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
559 msg_Dbg(p_access, " forward error correction 1/2");
560 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
561 msg_Dbg(p_access, " forward error correction 2/3");
562 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
563 msg_Dbg(p_access, " forward error correction 3/4");
564 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
565 msg_Dbg(p_access, " forward error correction 4/5");
566 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
567 msg_Dbg(p_access, " forward error correction 5/6");
568 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
569 msg_Dbg(p_access, " forward error correction 6/7");
570 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
571 msg_Dbg(p_access, " forward error correction 7/8");
572 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
573 msg_Dbg(p_access, " forward error correction 8/9");
574 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
575 msg_Dbg(p_access, " forward error correction auto");
576 if( p_frontend->info.caps & FE_CAN_QPSK)
577 msg_Dbg(p_access, " card can do QPSK");
578 if( p_frontend->info.caps & FE_CAN_QAM_16)
579 msg_Dbg(p_access, " card can do QAM 16");
580 if( p_frontend->info.caps & FE_CAN_QAM_32)
581 msg_Dbg(p_access, " card can do QAM 32");
582 if( p_frontend->info.caps & FE_CAN_QAM_64)
583 msg_Dbg(p_access, " card can do QAM 64");
584 if( p_frontend->info.caps & FE_CAN_QAM_128)
585 msg_Dbg(p_access, " card can do QAM 128");
586 if( p_frontend->info.caps & FE_CAN_QAM_256)
587 msg_Dbg(p_access, " card can do QAM 256");
588 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
589 msg_Dbg(p_access, " card can do QAM auto");
590 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
591 msg_Dbg(p_access, " transmission mode auto");
592 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
593 msg_Dbg(p_access, " bandwidth mode auto");
594 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
595 msg_Dbg(p_access, " guard interval mode auto");
596 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
597 msg_Dbg(p_access, " hierarchy mode auto");
598 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
599 msg_Dbg(p_access, " card can mute TS");
600 if( p_frontend->info.caps & FE_CAN_RECOVER)
601 msg_Dbg(p_access, " card can recover from a cable unplug");
602 msg_Dbg(p_access, "End of capability list");
607 /*****************************************************************************
608 * Decoding the DVB parameters (common)
609 *****************************************************************************/
610 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
613 fe_spectral_inversion_t fe_inversion = 0;
615 var_Get( p_access, "dvb-inversion", &val );
616 msg_Dbg( p_access, "using inversion=%d", val.i_int );
620 case 0: fe_inversion = INVERSION_OFF; break;
621 case 1: fe_inversion = INVERSION_ON; break;
622 case 2: fe_inversion = INVERSION_AUTO; break;
624 msg_Dbg( p_access, "dvb has inversion not set, using auto");
625 fe_inversion = INVERSION_AUTO;
631 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
633 fe_code_rate_t fe_fec = FEC_NONE;
635 msg_Dbg( p_access, "using fec=%d", i_val );
639 case 0: fe_fec = FEC_NONE; break;
640 case 1: fe_fec = FEC_1_2; break;
641 case 2: fe_fec = FEC_2_3; break;
642 case 3: fe_fec = FEC_3_4; break;
643 case 4: fe_fec = FEC_4_5; break;
644 case 5: fe_fec = FEC_5_6; break;
645 case 6: fe_fec = FEC_6_7; break;
646 case 7: fe_fec = FEC_7_8; break;
647 case 8: fe_fec = FEC_8_9; break;
648 case 9: fe_fec = FEC_AUTO; break;
652 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
658 static fe_modulation_t DecodeModulation( access_t *p_access )
661 fe_modulation_t fe_modulation = 0;
663 var_Get( p_access, "dvb-modulation", &val );
667 case -1: fe_modulation = QPSK; break;
668 case 0: fe_modulation = QAM_AUTO; break;
669 case 16: fe_modulation = QAM_16; break;
670 case 32: fe_modulation = QAM_32; break;
671 case 64: fe_modulation = QAM_64; break;
672 case 128: fe_modulation = QAM_128; break;
673 case 256: fe_modulation = QAM_256; break;
675 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
676 fe_modulation = QAM_AUTO;
679 return fe_modulation;
682 /*****************************************************************************
683 * FrontendSetQPSK : controls the FE device
684 *****************************************************************************/
685 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
688 fe_sec_voltage_t fe_voltage;
690 var_Get( p_access, "dvb-voltage", &val );
691 msg_Dbg( p_access, "using voltage=%d", val.i_int );
695 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
696 case 13: fe_voltage = SEC_VOLTAGE_13; break;
697 case 18: fe_voltage = SEC_VOLTAGE_18; break;
699 fe_voltage = SEC_VOLTAGE_OFF;
700 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
706 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
709 fe_sec_tone_mode_t fe_tone;
711 var_Get( p_access, "dvb-tone", &val );
712 msg_Dbg( p_access, "using tone=%d", val.i_int );
716 case 0: fe_tone = SEC_TONE_OFF; break;
717 case 1: fe_tone = SEC_TONE_ON; break;
719 fe_tone = SEC_TONE_OFF;
720 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
728 struct dvb_diseqc_master_cmd cmd;
732 static int DoDiseqc( access_t *p_access )
734 access_sys_t *p_sys = p_access->p_sys;
736 int i_frequency, i_lnb_slof;
737 fe_sec_voltage_t fe_voltage;
738 fe_sec_tone_mode_t fe_tone;
741 var_Get( p_access, "dvb-frequency", &val );
742 i_frequency = val.i_int;
743 var_Get( p_access, "dvb-lnb-slof", &val );
744 i_lnb_slof = val.i_int;
746 var_Get( p_access, "dvb-tone", &val );
747 if( val.i_int == -1 /* auto */ )
749 if( i_frequency >= i_lnb_slof )
753 var_Set( p_access, "dvb-tone", val );
756 fe_voltage = DecodeVoltage( p_access );
757 fe_tone = DecodeTone( p_access );
759 /* Switch off continuous tone. */
760 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
762 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
763 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
768 /* Configure LNB voltage. */
769 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
771 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
772 fe_voltage, i_err, strerror(errno) );
776 var_Get( p_access, "dvb-high-voltage", &val );
777 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
778 val.b_bool )) < 0 && val.b_bool )
781 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
782 val.b_bool, i_err, strerror(errno) );
785 /* Wait for at least 15 ms. */
788 var_Get( p_access, "dvb-satno", &val );
789 if( val.i_int > 0 && val.i_int < 5 )
791 /* digital satellite equipment control,
792 * specification is available from http://www.eutelsat.com/
795 /* 1.x compatible equipment */
796 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
798 /* param: high nibble: reset bits, low nibble set bits,
799 * bits are: option, position, polarization, band
801 cmd.cmd.msg[3] = 0xf0 /* reset bits */
802 | (((val.i_int - 1) * 4) & 0xc)
803 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
804 | (fe_tone == SEC_TONE_ON ? 1 : 0);
806 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
809 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
810 i_err, strerror(errno) );
814 msleep(15000 + cmd.wait * 1000);
816 /* A or B simple diseqc ("diseqc-compatible") */
817 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
818 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
820 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
821 i_err, strerror(errno) );
828 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
830 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
831 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
840 static int FrontendSetQPSK( access_t *p_access )
842 access_sys_t *p_sys = p_access->p_sys;
843 struct dvb_frontend_parameters fep;
846 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
848 /* Prepare the fep structure */
849 var_Get( p_access, "dvb-frequency", &val );
850 i_frequency = val.i_int;
852 var_Get( p_access, "dvb-lnb-lof1", &val );
853 if ( val.i_int == 0 )
855 /* Automatic mode. */
856 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
858 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
861 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
863 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
864 i_lnb_lof1 = 3650000;
866 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
868 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
870 i_lnb_lof1 = 5150000;
872 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
874 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
876 i_lnb_lof1 = 5950000;
878 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
880 msg_Dbg( p_access, "frequency %d is in Ku-band",
882 i_lnb_lof1 = 9750000;
883 i_lnb_lof2 = 10600000;
884 i_lnb_slof = 11700000;
888 msg_Err( p_access, "frequency %d is out of any known band",
890 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
891 "oscillator frequency" );
894 val.i_int = i_lnb_lof1;
895 var_Set( p_access, "dvb-lnb-lof1", val );
896 val.i_int = i_lnb_lof2;
897 var_Set( p_access, "dvb-lnb-lof2", val );
898 val.i_int = i_lnb_slof;
899 var_Set( p_access, "dvb-lnb-slof", val );
903 i_lnb_lof1 = val.i_int;
904 var_Get( p_access, "dvb-lnb-lof2", &val );
905 i_lnb_lof2 = val.i_int;
906 var_Get( p_access, "dvb-lnb-slof", &val );
907 i_lnb_slof = val.i_int;
910 if( i_lnb_slof && i_frequency >= i_lnb_slof )
912 i_frequency -= i_lnb_lof2;
916 i_frequency -= i_lnb_lof1;
918 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
920 fep.inversion = DecodeInversion( p_access );
922 var_Get( p_access, "dvb-srate", &val );
923 fep.u.qpsk.symbol_rate = val.i_int;
925 var_Get( p_access, "dvb-fec", &val );
926 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
928 if( DoDiseqc( p_access ) < 0 )
933 /* Empty the event queue */
936 struct dvb_frontend_event event;
937 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
938 && errno == EWOULDBLOCK )
942 /* Now send it all to the frontend device */
943 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
945 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
953 /*****************************************************************************
954 * FrontendSetQAM : controls the FE device
955 *****************************************************************************/
956 static int FrontendSetQAM( access_t *p_access )
958 access_sys_t *p_sys = p_access->p_sys;
959 struct dvb_frontend_parameters fep;
963 /* Prepare the fep structure */
965 var_Get( p_access, "dvb-frequency", &val );
966 fep.frequency = val.i_int;
968 fep.inversion = DecodeInversion( p_access );
970 var_Get( p_access, "dvb-srate", &val );
971 fep.u.qam.symbol_rate = val.i_int;
973 var_Get( p_access, "dvb-fec", &val );
974 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
976 fep.u.qam.modulation = DecodeModulation( p_access );
978 /* Empty the event queue */
981 struct dvb_frontend_event event;
982 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
983 && errno == EWOULDBLOCK )
987 /* Now send it all to the frontend device */
988 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
990 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
998 /*****************************************************************************
999 * FrontendSetOFDM : controls the FE device
1000 *****************************************************************************/
1001 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1004 fe_bandwidth_t fe_bandwidth = 0;
1006 var_Get( p_access, "dvb-bandwidth", &val );
1007 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
1011 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1012 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1013 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1014 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1016 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1017 fe_bandwidth = BANDWIDTH_AUTO;
1020 return fe_bandwidth;
1023 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1026 fe_transmit_mode_t fe_transmission = 0;
1028 var_Get( p_access, "dvb-transmission", &val );
1029 msg_Dbg( p_access, "using transmission=%d", val.i_int );
1033 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1034 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1035 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1037 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1038 fe_transmission = TRANSMISSION_MODE_AUTO;
1041 return fe_transmission;
1044 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1047 fe_guard_interval_t fe_guard = 0;
1049 var_Get( p_access, "dvb-guard", &val );
1050 msg_Dbg( p_access, "using guard=%d", val.i_int );
1054 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1055 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1056 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1057 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1058 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1060 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1061 fe_guard = GUARD_INTERVAL_AUTO;
1067 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1070 fe_hierarchy_t fe_hierarchy = 0;
1072 var_Get( p_access, "dvb-hierarchy", &val );
1073 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
1077 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1078 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1079 case 1: fe_hierarchy = HIERARCHY_1; break;
1080 case 2: fe_hierarchy = HIERARCHY_2; break;
1081 case 4: fe_hierarchy = HIERARCHY_4; break;
1083 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1084 fe_hierarchy = HIERARCHY_AUTO;
1087 return fe_hierarchy;
1090 static int FrontendSetOFDM( access_t * p_access )
1092 access_sys_t *p_sys = p_access->p_sys;
1093 struct dvb_frontend_parameters fep;
1097 /* Prepare the fep structure */
1099 var_Get( p_access, "dvb-frequency", &val );
1100 fep.frequency = val.i_int;
1102 fep.inversion = DecodeInversion( p_access );
1104 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1105 var_Get( p_access, "dvb-code-rate-hp", &val );
1106 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
1107 var_Get( p_access, "dvb-code-rate-lp", &val );
1108 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
1109 fep.u.ofdm.constellation = DecodeModulation( p_access );
1110 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1111 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1112 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1114 /* Empty the event queue */
1117 struct dvb_frontend_event event;
1118 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1119 && errno == EWOULDBLOCK )
1123 /* Now send it all to the frontend device */
1124 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1126 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
1139 /*****************************************************************************
1140 * DMXSetFilter : controls the demux to add a filter
1141 *****************************************************************************/
1142 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1144 struct dmx_pes_filter_params s_filter_params;
1146 unsigned int i_adapter, i_device;
1150 var_Get( p_access, "dvb-adapter", &val );
1151 i_adapter = val.i_int;
1152 var_Get( p_access, "dvb-device", &val );
1153 i_device = val.i_int;
1155 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1156 >= (int)sizeof(dmx) )
1158 msg_Err( p_access, "snprintf() truncated string for DMX" );
1159 dmx[sizeof(dmx) - 1] = '\0';
1162 msg_Dbg( p_access, "Opening device %s", dmx );
1163 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1165 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
1167 return VLC_EGENERIC;
1170 /* We fill the DEMUX structure : */
1171 s_filter_params.pid = i_pid;
1172 s_filter_params.input = DMX_IN_FRONTEND;
1173 s_filter_params.output = DMX_OUT_TS_TAP;
1174 s_filter_params.flags = DMX_IMMEDIATE_START;
1177 { /* First device */
1179 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1180 s_filter_params.pes_type = DMX_PES_VIDEO0;
1183 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1184 s_filter_params.pes_type = DMX_PES_AUDIO0;
1187 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1188 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1191 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1192 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1195 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1196 s_filter_params.pes_type = DMX_PES_PCR0;
1200 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1201 s_filter_params.pes_type = DMX_PES_VIDEO1;
1204 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1205 s_filter_params.pes_type = DMX_PES_AUDIO1;
1208 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1209 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1212 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1213 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1216 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1217 s_filter_params.pes_type = DMX_PES_PCR1;
1221 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1222 s_filter_params.pes_type = DMX_PES_VIDEO2;
1225 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1226 s_filter_params.pes_type = DMX_PES_AUDIO2;
1229 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1230 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1233 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1234 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1237 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1238 s_filter_params.pes_type = DMX_PES_PCR2;
1242 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1243 s_filter_params.pes_type = DMX_PES_VIDEO3;
1246 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1247 s_filter_params.pes_type = DMX_PES_AUDIO3;
1250 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1251 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1254 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1255 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1258 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1259 s_filter_params.pes_type = DMX_PES_PCR3;
1261 /* Usually used by Nova cards */
1264 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1265 s_filter_params.pes_type = DMX_PES_OTHER;
1269 /* We then give the order to the device : */
1270 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1272 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1274 return VLC_EGENERIC;
1279 /*****************************************************************************
1280 * DMXUnsetFilter : removes a filter
1281 *****************************************************************************/
1282 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1286 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1288 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1289 i_ret, strerror(errno) );
1293 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1303 /*****************************************************************************
1305 *****************************************************************************/
1306 int E_(DVROpen)( access_t * p_access )
1308 access_sys_t *p_sys = p_access->p_sys;
1309 unsigned int i_adapter, i_device;
1313 var_Get( p_access, "dvb-adapter", &val );
1314 i_adapter = val.i_int;
1315 var_Get( p_access, "dvb-device", &val );
1316 i_device = val.i_int;
1318 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1319 >= (int)sizeof(dvr) )
1321 msg_Err( p_access, "snprintf() truncated string for DVR" );
1322 dvr[sizeof(dvr) - 1] = '\0';
1325 msg_Dbg( p_access, "Opening device %s", dvr );
1326 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1328 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1330 return VLC_EGENERIC;
1333 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1335 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%s)",
1342 /*****************************************************************************
1344 *****************************************************************************/
1345 void E_(DVRClose)( access_t * p_access )
1347 access_sys_t *p_sys = p_access->p_sys;
1349 close( p_sys->i_handle );
1357 /*****************************************************************************
1359 *****************************************************************************/
1360 int E_(CAMOpen)( access_t *p_access )
1362 access_sys_t *p_sys = p_access->p_sys;
1364 int i_adapter, i_device;
1367 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1368 i_device = var_GetInteger( p_access, "dvb-device" );
1370 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1372 msg_Err( p_access, "snprintf() truncated string for CA" );
1373 ca[sizeof(ca) - 1] = '\0';
1375 memset( &caps, 0, sizeof( ca_caps_t ));
1377 msg_Dbg( p_access, "Opening device %s", ca );
1378 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1380 msg_Warn( p_access, "CAMInit: opening CAM device failed (%s)",
1382 p_sys->i_ca_handle = 0;
1383 return VLC_EGENERIC;
1386 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1388 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1389 close( p_sys->i_ca_handle );
1390 p_sys->i_ca_handle = 0;
1391 return VLC_EGENERIC;
1394 /* Output CA capabilities */
1395 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1396 caps.slot_num == 1 ? "slot" : "slots" );
1397 if ( caps.slot_type & CA_CI )
1398 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1399 if ( caps.slot_type & CA_CI_LINK )
1400 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1401 if ( caps.slot_type & CA_CI_PHYS )
1402 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1403 if ( caps.slot_type & CA_DESCR )
1404 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1405 if ( caps.slot_type & CA_SC )
1406 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1408 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1409 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1410 if ( caps.descr_type & CA_ECD )
1411 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1412 if ( caps.descr_type & CA_NDS )
1413 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1414 if ( caps.descr_type & CA_DSS )
1415 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1417 if ( caps.slot_num == 0 )
1419 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1420 close( p_sys->i_ca_handle );
1421 p_sys->i_ca_handle = 0;
1422 return VLC_EGENERIC;
1425 p_sys->i_ca_type = caps.slot_type;
1426 if ( !(caps.slot_type & CA_CI_LINK) &&
1427 !(caps.slot_type & CA_CI) )
1429 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1430 close( p_sys->i_ca_handle );
1431 p_sys->i_ca_handle = 0;
1432 return VLC_EGENERIC;
1435 p_sys->i_nb_slots = caps.slot_num;
1436 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1437 memset( p_sys->pb_slot_mmi_expected, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1438 memset( p_sys->pb_slot_mmi_undisplayed, 0,
1439 sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1441 return E_(en50221_Init)( p_access );
1444 /*****************************************************************************
1446 *****************************************************************************/
1447 int E_(CAMPoll)( access_t * p_access )
1449 access_sys_t *p_sys = p_access->p_sys;
1450 int i_ret = VLC_EGENERIC;
1452 if ( p_sys->i_ca_handle == 0 )
1454 return VLC_EGENERIC;
1457 switch( p_sys->i_ca_type )
1460 i_ret = E_(en50221_Poll)( p_access );
1463 i_ret = VLC_SUCCESS;
1466 msg_Err( p_access, "CAMPoll: This should not happen" );
1474 /*****************************************************************************
1476 *****************************************************************************/
1477 void E_(CAMStatus)( access_t * p_access )
1479 access_sys_t *p_sys = p_access->p_sys;
1484 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1486 /* Check if we have an undisplayed MMI message : in that case we ignore
1487 * the user input to avoid confusing the CAM. */
1488 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1490 if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == VLC_TRUE )
1492 p_sys->psz_request = NULL;
1494 "ignoring user request because of a new MMI object" );
1500 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1502 /* We have a mission to accomplish. */
1503 en50221_mmi_object_t mmi_object;
1504 char *psz_request = p_sys->psz_request;
1505 char psz_value[255];
1507 vlc_bool_t b_ok = VLC_FALSE;
1509 p_sys->psz_request = NULL;
1511 if ( E_(HTTPExtractValue)( psz_request, "slot", psz_value,
1512 sizeof(psz_value) ) == NULL )
1514 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1517 i_slot = atoi(psz_value);
1519 if ( E_(HTTPExtractValue)( psz_request, "open", psz_value,
1520 sizeof(psz_value) ) != NULL )
1522 E_(en50221_OpenMMI)( p_access, i_slot );
1526 if ( E_(HTTPExtractValue)( psz_request, "close", psz_value,
1527 sizeof(psz_value) ) != NULL )
1529 E_(en50221_CloseMMI)( p_access, i_slot );
1533 if ( E_(HTTPExtractValue)( psz_request, "cancel", psz_value,
1534 sizeof(psz_value) ) == NULL )
1539 if ( E_(HTTPExtractValue)( psz_request, "type", psz_value,
1540 sizeof(psz_value) ) == NULL )
1542 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1546 if ( !strcmp( psz_value, "enq" ) )
1548 mmi_object.i_object_type = EN50221_MMI_ANSW;
1549 mmi_object.u.answ.b_ok = b_ok;
1550 if ( b_ok == VLC_FALSE )
1552 mmi_object.u.answ.psz_answ = strdup("");
1556 if ( E_(HTTPExtractValue)( psz_request, "answ", psz_value,
1557 sizeof(psz_value) ) == NULL )
1559 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1563 mmi_object.u.answ.psz_answ = strdup(psz_value);
1568 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1569 if ( b_ok == VLC_FALSE )
1571 mmi_object.u.menu_answ.i_choice = 0;
1575 if ( E_(HTTPExtractValue)( psz_request, "choice", psz_value,
1576 sizeof(psz_value) ) == NULL )
1577 mmi_object.u.menu_answ.i_choice = 0;
1579 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1583 E_(en50221_SendMMIObject)( p_access, i_slot, &mmi_object );
1587 /* Check that we have all necessary MMI information. */
1588 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1590 if ( p_sys->pb_slot_mmi_expected[i_slot] == VLC_TRUE )
1594 p = p_sys->psz_mmi_info = malloc( 10000 );
1596 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1598 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n",
1603 /* Output CA capabilities */
1604 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1605 caps.slot_num == 1 ? "slot" : "slots" );
1606 #define CHECK_CAPS( x, s ) \
1607 if ( caps.slot_type & (CA_##x) ) \
1608 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1610 CHECK_CAPS( CI, "CI high level interface" );
1611 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1612 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1613 CHECK_CAPS( DESCR, "built-in descrambler" );
1614 CHECK_CAPS( SC, "simple smartcard interface" );
1617 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1618 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1619 #define CHECK_DESC( x ) \
1620 if ( caps.descr_type & (CA_##x) ) \
1621 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1628 p += sprintf( p, "</table>" );
1630 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1632 ca_slot_info_t sinfo;
1634 p_sys->pb_slot_mmi_undisplayed[i_slot] = VLC_FALSE;
1635 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1638 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1640 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n",
1645 #define CHECK_TYPE( x, s ) \
1646 if ( sinfo.type & (CA_##x) ) \
1647 p += sprintf( p, s );
1649 CHECK_TYPE( CI, "high level, " );
1650 CHECK_TYPE( CI_LINK, "link layer level, " );
1651 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1654 if ( sinfo.flags & CA_CI_MODULE_READY )
1656 en50221_mmi_object_t *p_object = E_(en50221_GetMMIObject)( p_access,
1659 p += sprintf( p, "module present and ready<p>\n" );
1660 p += sprintf( p, "<form action=index.html method=get>\n" );
1661 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1664 if ( p_object == NULL )
1666 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1670 switch ( p_object->i_object_type )
1672 case EN50221_MMI_ENQ:
1673 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1674 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1675 p_object->u.enq.psz_text );
1676 if ( p_object->u.enq.b_blind == VLC_FALSE )
1677 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1679 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1682 case EN50221_MMI_MENU:
1683 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1684 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1685 p_object->u.menu.psz_title );
1686 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1687 p_object->u.menu.psz_subtitle );
1688 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1689 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1690 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1691 p_object->u.menu.psz_bottom );
1694 case EN50221_MMI_LIST:
1695 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1696 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1697 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1698 p_object->u.menu.psz_title );
1699 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1700 p_object->u.menu.psz_subtitle );
1701 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1702 p += sprintf( p, "%s<br>\n",
1703 p_object->u.menu.ppsz_choices[i] );
1704 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1705 p_object->u.menu.psz_bottom );
1709 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1712 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1713 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1714 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1716 p += sprintf( p, "</form>\n" );
1718 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1719 p += sprintf( p, "module present, not ready<br>\n" );
1721 p += sprintf( p, "module not present<br>\n" );
1725 vlc_mutex_lock( &p_sys->httpd_mutex );
1726 p_sys->b_request_mmi_info = VLC_FALSE;
1727 vlc_cond_signal( &p_sys->httpd_cond );
1728 vlc_mutex_unlock( &p_sys->httpd_mutex );
1732 /*****************************************************************************
1734 *****************************************************************************/
1735 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1737 access_sys_t *p_sys = p_access->p_sys;
1739 if ( p_sys->i_ca_handle == 0 )
1741 return VLC_EGENERIC;
1744 E_(en50221_SetCAPMT)( p_access, p_pmt );
1749 /*****************************************************************************
1751 *****************************************************************************/
1752 void E_(CAMClose)( access_t * p_access )
1754 access_sys_t *p_sys = p_access->p_sys;
1756 E_(en50221_End)( p_access );
1758 if ( p_sys->i_ca_handle )
1760 close( p_sys->i_ca_handle );