1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2004 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"
71 fe_status_t i_last_status;
72 struct dvb_frontend_info info;
75 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
77 /* Local prototypes */
78 static int FrontendInfo( access_t * );
79 static int FrontendSetQPSK( access_t * );
80 static int FrontendSetQAM( access_t * );
81 static int FrontendSetOFDM( access_t * );
83 /*****************************************************************************
84 * FrontendOpen : Determine frontend device information and capabilities
85 *****************************************************************************/
86 int E_(FrontendOpen)( access_t *p_access )
88 access_sys_t *p_sys = p_access->p_sys;
89 frontend_t * p_frontend;
90 unsigned int i_adapter, i_device;
94 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
95 i_device = var_GetInteger( p_access, "dvb-device" );
96 b_probe = var_GetBool( p_access, "dvb-probe" );
98 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
100 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
101 frontend[sizeof(frontend) - 1] = '\0';
104 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
106 msg_Dbg( p_access, "Opening device %s", frontend );
107 if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
109 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
117 char * psz_expected = NULL;
120 if( FrontendInfo( p_access ) < 0 )
122 close( p_sys->i_frontend_handle );
127 switch( p_frontend->info.type )
139 psz_real = "unknown";
143 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
144 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
145 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
146 (p_frontend->info.type != FE_QPSK) )
148 psz_expected = "DVB-S";
150 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
151 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
152 (p_frontend->info.type != FE_QAM) )
154 psz_expected = "DVB-C";
156 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
157 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
158 (p_frontend->info.type != FE_OFDM) )
160 psz_expected = "DVB-T";
163 if( psz_expected != NULL )
165 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
166 psz_expected, psz_real );
167 close( p_sys->i_frontend_handle );
172 else /* no frontend probing is done so use default border values. */
174 msg_Dbg( p_access, "using default values for frontend info" );
176 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
177 p_frontend->info.type = FE_QPSK;
178 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
179 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
180 p_frontend->info.type = FE_QPSK;
181 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
182 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
183 p_frontend->info.type = FE_QAM;
184 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
185 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
186 p_frontend->info.type = FE_OFDM;
192 /*****************************************************************************
193 * FrontendClose : Close the frontend
194 *****************************************************************************/
195 void E_(FrontendClose)( access_t *p_access )
197 access_sys_t *p_sys = p_access->p_sys;
199 if( p_sys->p_frontend )
201 close( p_sys->i_frontend_handle );
202 free( p_sys->p_frontend );
204 p_sys->p_frontend = NULL;
208 /*****************************************************************************
209 * FrontendSet : Tune !
210 *****************************************************************************/
211 int E_(FrontendSet)( access_t *p_access )
213 access_sys_t *p_sys = p_access->p_sys;
215 switch( p_sys->p_frontend->info.type )
219 if( FrontendSetQPSK( p_access ) < 0 )
221 msg_Err( p_access, "DVB-S: tuning failed" );
228 if( FrontendSetQAM( p_access ) < 0 )
230 msg_Err( p_access, "DVB-C: tuning failed" );
237 if( FrontendSetOFDM( p_access ) < 0 )
239 msg_Err( p_access, "DVB-T: tuning failed" );
245 msg_Err( p_access, "Could not determine frontend type on %s",
246 p_sys->p_frontend->info.name );
249 p_sys->p_frontend->i_last_status = 0;
250 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
254 /*****************************************************************************
255 * FrontendPoll : Poll for frontend events
256 *****************************************************************************/
257 void E_(FrontendPoll)( access_t *p_access )
259 access_sys_t *p_sys = p_access->p_sys;
260 frontend_t * p_frontend = p_sys->p_frontend;
261 struct dvb_frontend_event event;
262 fe_status_t i_status, i_diff;
266 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
270 if( errno == EWOULDBLOCK )
273 msg_Err( p_access, "reading frontend status failed (%d) %s",
274 i_ret, strerror(errno) );
278 i_status = event.status;
279 i_diff = i_status ^ p_frontend->i_last_status;
280 p_frontend->i_last_status = i_status;
284 if ( i_diff & (x) ) \
286 if ( i_status & (x) )
289 IF_UP( FE_HAS_SIGNAL )
290 msg_Dbg( p_access, "frontend has acquired signal" );
292 msg_Dbg( p_access, "frontend has lost signal" );
294 IF_UP( FE_HAS_CARRIER )
295 msg_Dbg( p_access, "frontend has acquired carrier" );
297 msg_Dbg( p_access, "frontend has lost carrier" );
299 IF_UP( FE_HAS_VITERBI )
300 msg_Dbg( p_access, "frontend has acquired stable FEC" );
302 msg_Dbg( p_access, "frontend has lost FEC" );
305 msg_Dbg( p_access, "frontend has acquired sync" );
307 msg_Dbg( p_access, "frontend has lost sync" );
312 msg_Dbg( p_access, "frontend has acquired lock" );
313 p_sys->i_frontend_timeout = 0;
315 /* Read some statistics */
316 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
317 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
318 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
319 msg_Dbg( p_access, "- Signal strength: %d", i_value );
320 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
321 msg_Dbg( p_access, "- SNR: %d", i_value );
325 msg_Dbg( p_access, "frontend has lost lock" );
326 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
331 /* The frontend was reinited. */
332 msg_Warn( p_access, "reiniting frontend");
333 E_(FrontendSet)( p_access );
338 /*****************************************************************************
339 * FrontendInfo : Return information about given frontend
340 *****************************************************************************/
341 static int FrontendInfo( access_t *p_access )
343 access_sys_t *p_sys = p_access->p_sys;
344 frontend_t *p_frontend = p_sys->p_frontend;
347 /* Determine type of frontend */
348 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
349 &p_frontend->info )) < 0 )
351 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
356 /* Print out frontend capabilities. */
357 msg_Dbg(p_access, "Frontend Info:" );
358 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
359 switch( p_frontend->info.type )
362 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
365 msg_Dbg( p_access, " type = QAM (DVB-C)" );
368 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
370 #if 0 /* DVB_API_VERSION == 3 */
372 msg_Dbg(p_access, " type = MEMORY" );
375 msg_Dbg(p_access, " type = NETWORK" );
379 msg_Err( p_access, " unknown frontend type (%d)",
380 p_frontend->info.type );
383 msg_Dbg(p_access, " frequency_min = %u (kHz)",
384 p_frontend->info.frequency_min);
385 msg_Dbg(p_access, " frequency_max = %u (kHz)",
386 p_frontend->info.frequency_max);
387 msg_Dbg(p_access, " frequency_stepsize = %u",
388 p_frontend->info.frequency_stepsize);
389 msg_Dbg(p_access, " frequency_tolerance = %u",
390 p_frontend->info.frequency_tolerance);
391 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
392 p_frontend->info.symbol_rate_min);
393 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
394 p_frontend->info.symbol_rate_max);
395 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
396 p_frontend->info.symbol_rate_tolerance);
397 msg_Dbg(p_access, " notifier_delay (ms) = %u",
398 p_frontend->info.notifier_delay );
400 msg_Dbg(p_access, "Frontend Info capability list:");
401 if( p_frontend->info.caps & FE_IS_STUPID)
402 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
403 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
404 msg_Dbg(p_access, " inversion auto");
405 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
406 msg_Dbg(p_access, " forward error correction 1/2");
407 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
408 msg_Dbg(p_access, " forward error correction 2/3");
409 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
410 msg_Dbg(p_access, " forward error correction 3/4");
411 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
412 msg_Dbg(p_access, " forward error correction 4/5");
413 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
414 msg_Dbg(p_access, " forward error correction 5/6");
415 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
416 msg_Dbg(p_access, " forward error correction 6/7");
417 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
418 msg_Dbg(p_access, " forward error correction 7/8");
419 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
420 msg_Dbg(p_access, " forward error correction 8/9");
421 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
422 msg_Dbg(p_access, " forward error correction auto");
423 if( p_frontend->info.caps & FE_CAN_QPSK)
424 msg_Dbg(p_access, " card can do QPSK");
425 if( p_frontend->info.caps & FE_CAN_QAM_16)
426 msg_Dbg(p_access, " card can do QAM 16");
427 if( p_frontend->info.caps & FE_CAN_QAM_32)
428 msg_Dbg(p_access, " card can do QAM 32");
429 if( p_frontend->info.caps & FE_CAN_QAM_64)
430 msg_Dbg(p_access, " card can do QAM 64");
431 if( p_frontend->info.caps & FE_CAN_QAM_128)
432 msg_Dbg(p_access, " card can do QAM 128");
433 if( p_frontend->info.caps & FE_CAN_QAM_256)
434 msg_Dbg(p_access, " card can do QAM 256");
435 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
436 msg_Dbg(p_access, " card can do QAM auto");
437 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
438 msg_Dbg(p_access, " transmission mode auto");
439 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
440 msg_Dbg(p_access, " bandwidth mode auto");
441 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
442 msg_Dbg(p_access, " guard interval mode auto");
443 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
444 msg_Dbg(p_access, " hierarchy mode auto");
445 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
446 msg_Dbg(p_access, " card can mute TS");
447 if( p_frontend->info.caps & FE_CAN_RECOVER)
448 msg_Dbg(p_access, " card can recover from a cable unplug");
449 msg_Dbg(p_access, "End of capability list");
454 /*****************************************************************************
455 * Decoding the DVB parameters (common)
456 *****************************************************************************/
457 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
460 fe_spectral_inversion_t fe_inversion = 0;
462 var_Get( p_access, "dvb-inversion", &val );
463 msg_Dbg( p_access, "using inversion=%d", val.i_int );
467 case 0: fe_inversion = INVERSION_OFF; break;
468 case 1: fe_inversion = INVERSION_ON; break;
469 case 2: fe_inversion = INVERSION_AUTO; break;
471 msg_Dbg( p_access, "dvb has inversion not set, using auto");
472 fe_inversion = INVERSION_AUTO;
478 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
480 fe_code_rate_t fe_fec = FEC_NONE;
482 msg_Dbg( p_access, "using fec=%d", i_val );
486 case 0: fe_fec = FEC_NONE; break;
487 case 1: fe_fec = FEC_1_2; break;
488 case 2: fe_fec = FEC_2_3; break;
489 case 3: fe_fec = FEC_3_4; break;
490 case 4: fe_fec = FEC_4_5; break;
491 case 5: fe_fec = FEC_5_6; break;
492 case 6: fe_fec = FEC_6_7; break;
493 case 7: fe_fec = FEC_7_8; break;
494 case 8: fe_fec = FEC_8_9; break;
495 case 9: fe_fec = FEC_AUTO; break;
499 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
505 static fe_modulation_t DecodeModulation( access_t *p_access )
508 fe_modulation_t fe_modulation = 0;
510 var_Get( p_access, "dvb-modulation", &val );
514 case -1: fe_modulation = QPSK; break;
515 case 0: fe_modulation = QAM_AUTO; break;
516 case 16: fe_modulation = QAM_16; break;
517 case 32: fe_modulation = QAM_32; break;
518 case 64: fe_modulation = QAM_64; break;
519 case 128: fe_modulation = QAM_128; break;
520 case 256: fe_modulation = QAM_256; break;
522 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
523 fe_modulation = QAM_AUTO;
526 return fe_modulation;
529 /*****************************************************************************
530 * FrontendSetQPSK : controls the FE device
531 *****************************************************************************/
532 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
535 fe_sec_voltage_t fe_voltage;
537 var_Get( p_access, "dvb-voltage", &val );
538 msg_Dbg( p_access, "using voltage=%d", val.i_int );
542 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
543 case 13: fe_voltage = SEC_VOLTAGE_13; break;
544 case 18: fe_voltage = SEC_VOLTAGE_18; break;
546 fe_voltage = SEC_VOLTAGE_OFF;
547 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
553 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
556 fe_sec_tone_mode_t fe_tone;
558 var_Get( p_access, "dvb-tone", &val );
559 msg_Dbg( p_access, "using tone=%d", val.i_int );
563 case 0: fe_tone = SEC_TONE_OFF; break;
564 case 1: fe_tone = SEC_TONE_ON; break;
566 fe_tone = SEC_TONE_OFF;
567 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
575 struct dvb_diseqc_master_cmd cmd;
579 static int DoDiseqc( access_t *p_access )
581 access_sys_t *p_sys = p_access->p_sys;
583 int i_frequency, i_lnb_slof;
584 fe_sec_voltage_t fe_voltage;
585 fe_sec_tone_mode_t fe_tone;
588 var_Get( p_access, "dvb-frequency", &val );
589 i_frequency = val.i_int;
590 var_Get( p_access, "dvb-lnb-slof", &val );
591 i_lnb_slof = val.i_int;
593 var_Get( p_access, "dvb-tone", &val );
594 if( val.i_int == -1 /* auto */ )
596 if( i_frequency >= i_lnb_slof )
600 var_Set( p_access, "dvb-tone", val );
603 fe_voltage = DecodeVoltage( p_access );
604 fe_tone = DecodeTone( p_access );
606 /* Switch off continuous tone. */
607 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
609 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
610 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
615 /* Configure LNB voltage. */
616 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
618 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
619 fe_voltage, i_err, strerror(errno) );
623 var_Get( p_access, "dvb-high-voltage", &val );
624 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
625 val.b_bool )) < 0 && val.b_bool )
628 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
629 val.b_bool, i_err, strerror(errno) );
632 /* Wait for at least 15 ms. */
635 var_Get( p_access, "dvb-satno", &val );
636 if( val.i_int > 0 && val.i_int < 5 )
638 /* digital satellite equipment control,
639 * specification is available from http://www.eutelsat.com/
642 /* 1.x compatible equipment */
643 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
645 /* param: high nibble: reset bits, low nibble set bits,
646 * bits are: option, position, polarization, band
648 cmd.cmd.msg[3] = 0xf0 /* reset bits */
649 | (((val.i_int - 1) * 4) & 0xc)
650 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
651 | (fe_tone == SEC_TONE_ON ? 1 : 0);
653 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
656 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
657 i_err, strerror(errno) );
661 msleep(15000 + cmd.wait * 1000);
663 /* A or B simple diseqc ("diseqc-compatible") */
664 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
665 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
667 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
668 i_err, strerror(errno) );
675 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
677 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
678 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
687 static int FrontendSetQPSK( access_t *p_access )
689 access_sys_t *p_sys = p_access->p_sys;
690 struct dvb_frontend_parameters fep;
693 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
695 /* Prepare the fep structure */
696 var_Get( p_access, "dvb-frequency", &val );
697 i_frequency = val.i_int;
699 var_Get( p_access, "dvb-lnb-lof1", &val );
700 if ( val.i_int == 0 )
702 /* Automatic mode. */
703 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
705 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
708 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
710 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
711 i_lnb_lof1 = 3650000;
713 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
715 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
717 i_lnb_lof1 = 5150000;
719 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
721 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
723 i_lnb_lof1 = 5950000;
725 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
727 msg_Dbg( p_access, "frequency %d is in Ku-band",
729 i_lnb_lof1 = 9750000;
730 i_lnb_lof2 = 10600000;
731 i_lnb_slof = 11700000;
735 msg_Err( p_access, "frequency %d is out of any known band",
737 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
738 "oscillator frequency" );
741 val.i_int = i_lnb_lof1;
742 var_Set( p_access, "dvb-lnb-lof1", val );
743 val.i_int = i_lnb_lof2;
744 var_Set( p_access, "dvb-lnb-lof2", val );
745 val.i_int = i_lnb_slof;
746 var_Set( p_access, "dvb-lnb-slof", val );
750 i_lnb_lof1 = val.i_int;
751 var_Get( p_access, "dvb-lnb-lof2", &val );
752 i_lnb_lof2 = val.i_int;
753 var_Get( p_access, "dvb-lnb-slof", &val );
754 i_lnb_slof = val.i_int;
757 if( i_lnb_slof && i_frequency >= i_lnb_slof )
759 i_frequency -= i_lnb_lof2;
763 i_frequency -= i_lnb_lof1;
765 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
767 fep.inversion = DecodeInversion( p_access );
769 var_Get( p_access, "dvb-srate", &val );
770 fep.u.qpsk.symbol_rate = val.i_int;
772 var_Get( p_access, "dvb-fec", &val );
773 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
775 if( DoDiseqc( p_access ) < 0 )
780 /* Empty the event queue */
783 struct dvb_frontend_event event;
784 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
785 && errno == EWOULDBLOCK )
789 /* Now send it all to the frontend device */
790 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
792 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
800 /*****************************************************************************
801 * FrontendSetQAM : controls the FE device
802 *****************************************************************************/
803 static int FrontendSetQAM( access_t *p_access )
805 access_sys_t *p_sys = p_access->p_sys;
806 struct dvb_frontend_parameters fep;
810 /* Prepare the fep structure */
812 var_Get( p_access, "dvb-frequency", &val );
813 fep.frequency = val.i_int;
815 fep.inversion = DecodeInversion( p_access );
817 var_Get( p_access, "dvb-srate", &val );
818 fep.u.qam.symbol_rate = val.i_int;
820 var_Get( p_access, "dvb-fec", &val );
821 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
823 fep.u.qam.modulation = DecodeModulation( p_access );
825 /* Empty the event queue */
828 struct dvb_frontend_event event;
829 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
830 && errno == EWOULDBLOCK )
834 /* Now send it all to the frontend device */
835 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
837 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
845 /*****************************************************************************
846 * FrontendSetOFDM : controls the FE device
847 *****************************************************************************/
848 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
851 fe_bandwidth_t fe_bandwidth = 0;
853 var_Get( p_access, "dvb-bandwidth", &val );
854 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
858 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
859 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
860 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
861 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
863 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
864 fe_bandwidth = BANDWIDTH_AUTO;
870 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
873 fe_transmit_mode_t fe_transmission = 0;
875 var_Get( p_access, "dvb-transmission", &val );
876 msg_Dbg( p_access, "using transmission=%d", val.i_int );
880 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
881 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
882 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
884 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
885 fe_transmission = TRANSMISSION_MODE_AUTO;
888 return fe_transmission;
891 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
894 fe_guard_interval_t fe_guard = 0;
896 var_Get( p_access, "dvb-guard", &val );
897 msg_Dbg( p_access, "using guard=%d", val.i_int );
901 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
902 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
903 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
904 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
905 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
907 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
908 fe_guard = GUARD_INTERVAL_AUTO;
914 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
917 fe_hierarchy_t fe_hierarchy = 0;
919 var_Get( p_access, "dvb-hierarchy", &val );
920 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
924 case -1: fe_hierarchy = HIERARCHY_NONE; break;
925 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
926 case 1: fe_hierarchy = HIERARCHY_1; break;
927 case 2: fe_hierarchy = HIERARCHY_2; break;
928 case 4: fe_hierarchy = HIERARCHY_4; break;
930 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
931 fe_hierarchy = HIERARCHY_AUTO;
937 static int FrontendSetOFDM( access_t * p_access )
939 access_sys_t *p_sys = p_access->p_sys;
940 struct dvb_frontend_parameters fep;
944 /* Prepare the fep structure */
946 var_Get( p_access, "dvb-frequency", &val );
947 fep.frequency = val.i_int;
949 fep.inversion = DecodeInversion( p_access );
951 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
952 var_Get( p_access, "dvb-code-rate-hp", &val );
953 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
954 var_Get( p_access, "dvb-code-rate-lp", &val );
955 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
956 fep.u.ofdm.constellation = DecodeModulation( p_access );
957 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
958 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
959 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
961 /* Empty the event queue */
964 struct dvb_frontend_event event;
965 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
966 && errno == EWOULDBLOCK )
970 /* Now send it all to the frontend device */
971 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
973 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
986 /*****************************************************************************
987 * DMXSetFilter : controls the demux to add a filter
988 *****************************************************************************/
989 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
991 struct dmx_pes_filter_params s_filter_params;
993 unsigned int i_adapter, i_device;
997 var_Get( p_access, "dvb-adapter", &val );
998 i_adapter = val.i_int;
999 var_Get( p_access, "dvb-device", &val );
1000 i_device = val.i_int;
1002 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1003 >= (int)sizeof(dmx) )
1005 msg_Err( p_access, "snprintf() truncated string for DMX" );
1006 dmx[sizeof(dmx) - 1] = '\0';
1009 msg_Dbg( p_access, "Opening device %s", dmx );
1010 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1012 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
1014 return VLC_EGENERIC;
1017 /* We fill the DEMUX structure : */
1018 s_filter_params.pid = i_pid;
1019 s_filter_params.input = DMX_IN_FRONTEND;
1020 s_filter_params.output = DMX_OUT_TS_TAP;
1021 s_filter_params.flags = DMX_IMMEDIATE_START;
1024 { /* First device */
1026 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1027 s_filter_params.pes_type = DMX_PES_VIDEO0;
1030 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1031 s_filter_params.pes_type = DMX_PES_AUDIO0;
1034 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1035 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1038 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1039 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1042 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1043 s_filter_params.pes_type = DMX_PES_PCR0;
1047 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1048 s_filter_params.pes_type = DMX_PES_VIDEO1;
1051 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1052 s_filter_params.pes_type = DMX_PES_AUDIO1;
1055 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1056 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1059 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1060 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1063 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1064 s_filter_params.pes_type = DMX_PES_PCR1;
1068 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1069 s_filter_params.pes_type = DMX_PES_VIDEO2;
1072 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1073 s_filter_params.pes_type = DMX_PES_AUDIO2;
1076 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1077 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1080 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1081 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1084 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1085 s_filter_params.pes_type = DMX_PES_PCR2;
1089 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1090 s_filter_params.pes_type = DMX_PES_VIDEO3;
1093 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1094 s_filter_params.pes_type = DMX_PES_AUDIO3;
1097 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1098 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1101 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1102 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1105 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1106 s_filter_params.pes_type = DMX_PES_PCR3;
1108 /* Usually used by Nova cards */
1111 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1112 s_filter_params.pes_type = DMX_PES_OTHER;
1116 /* We then give the order to the device : */
1117 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1119 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1121 return VLC_EGENERIC;
1126 /*****************************************************************************
1127 * DMXUnsetFilter : removes a filter
1128 *****************************************************************************/
1129 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1133 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1135 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1136 i_ret, strerror(errno) );
1140 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1150 /*****************************************************************************
1152 *****************************************************************************/
1153 int E_(DVROpen)( access_t * p_access )
1155 access_sys_t *p_sys = p_access->p_sys;
1156 unsigned int i_adapter, i_device;
1160 var_Get( p_access, "dvb-adapter", &val );
1161 i_adapter = val.i_int;
1162 var_Get( p_access, "dvb-device", &val );
1163 i_device = val.i_int;
1165 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1166 >= (int)sizeof(dvr) )
1168 msg_Err( p_access, "snprintf() truncated string for DVR" );
1169 dvr[sizeof(dvr) - 1] = '\0';
1172 msg_Dbg( p_access, "Opening device %s", dvr );
1173 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1175 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1177 return VLC_EGENERIC;
1180 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1182 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%s)",
1189 /*****************************************************************************
1191 *****************************************************************************/
1192 void E_(DVRClose)( access_t * p_access )
1194 access_sys_t *p_sys = p_access->p_sys;
1196 close( p_sys->i_handle );
1204 /*****************************************************************************
1206 *****************************************************************************/
1207 int E_(CAMOpen)( access_t *p_access )
1209 access_sys_t *p_sys = p_access->p_sys;
1211 int i_adapter, i_device, i_slot;
1214 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1215 i_device = var_GetInteger( p_access, "dvb-device" );
1217 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1219 msg_Err( p_access, "snprintf() truncated string for CA" );
1220 ca[sizeof(ca) - 1] = '\0';
1223 msg_Dbg( p_access, "Opening device %s", ca );
1224 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1226 msg_Warn( p_access, "CAMInit: opening CAM device failed (%s)",
1228 p_sys->i_ca_handle = 0;
1229 return VLC_EGENERIC;
1232 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1234 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1235 close( p_sys->i_ca_handle );
1236 p_sys->i_ca_handle = 0;
1237 return VLC_EGENERIC;
1240 /* Output CA capabilities */
1241 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1242 caps.slot_num == 1 ? "slot" : "slots" );
1243 if ( caps.slot_type & CA_CI )
1244 msg_Dbg( p_access, "CAMInit: CI high level interface type (not supported)" );
1245 if ( caps.slot_type & CA_CI_LINK )
1246 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1247 if ( caps.slot_type & CA_CI_PHYS )
1248 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1249 if ( caps.slot_type & CA_DESCR )
1250 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1251 if ( caps.slot_type & CA_SC )
1252 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1254 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1255 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1256 if ( caps.descr_type & CA_ECD )
1257 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1258 if ( caps.descr_type & CA_NDS )
1259 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1260 if ( caps.descr_type & CA_DSS )
1261 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1263 if ( caps.slot_num == 0 )
1265 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1266 close( p_sys->i_ca_handle );
1267 p_sys->i_ca_handle = 0;
1268 return VLC_EGENERIC;
1271 if ( !(caps.slot_type & CA_CI_LINK) )
1273 msg_Err( p_access, "CAMInit: no compatible CAM module" );
1274 close( p_sys->i_ca_handle );
1275 p_sys->i_ca_handle = 0;
1276 return VLC_EGENERIC;
1279 p_sys->i_nb_slots = caps.slot_num;
1280 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1282 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1284 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1286 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1290 p_sys->i_ca_timeout = 100000;
1291 /* Wait a bit otherwise it doesn't initialize properly... */
1297 /*****************************************************************************
1299 *****************************************************************************/
1300 int E_(CAMPoll)( access_t * p_access )
1302 access_sys_t *p_sys = p_access->p_sys;
1304 if ( p_sys->i_ca_handle == 0 )
1306 return VLC_EGENERIC;
1309 return E_(en50221_Poll)( p_access );
1312 /*****************************************************************************
1314 *****************************************************************************/
1315 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1317 access_sys_t *p_sys = p_access->p_sys;
1319 if ( p_sys->i_ca_handle == 0 )
1321 return VLC_EGENERIC;
1324 E_(en50221_SetCAPMT)( p_access, p_pmt );
1329 /*****************************************************************************
1331 *****************************************************************************/
1332 void E_(CAMClose)( access_t * p_access )
1334 access_sys_t *p_sys = p_access->p_sys;
1336 E_(en50221_End)( p_access );
1338 if ( p_sys->i_ca_handle )
1340 close( p_sys->i_ca_handle );