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 #define RESET_CAM_SLOTS 1 /* Do we want to reset cam upon opening */
79 /* Local prototypes */
80 static int FrontendInfo( access_t * );
81 static int FrontendSetQPSK( access_t * );
82 static int FrontendSetQAM( access_t * );
83 static int FrontendSetOFDM( access_t * );
85 /*****************************************************************************
86 * FrontendOpen : Determine frontend device information and capabilities
87 *****************************************************************************/
88 int E_(FrontendOpen)( access_t *p_access )
90 access_sys_t *p_sys = p_access->p_sys;
91 frontend_t * p_frontend;
92 unsigned int i_adapter, i_device;
96 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
97 i_device = var_GetInteger( p_access, "dvb-device" );
98 b_probe = var_GetBool( p_access, "dvb-probe" );
100 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
102 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
103 frontend[sizeof(frontend) - 1] = '\0';
106 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
108 msg_Dbg( p_access, "Opening device %s", frontend );
109 if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
111 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
119 char * psz_expected = NULL;
122 if( FrontendInfo( p_access ) < 0 )
124 close( p_sys->i_frontend_handle );
129 switch( p_frontend->info.type )
141 psz_real = "unknown";
145 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
146 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
147 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
148 (p_frontend->info.type != FE_QPSK) )
150 psz_expected = "DVB-S";
152 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
153 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
154 (p_frontend->info.type != FE_QAM) )
156 psz_expected = "DVB-C";
158 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
159 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
160 (p_frontend->info.type != FE_OFDM) )
162 psz_expected = "DVB-T";
165 if( psz_expected != NULL )
167 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
168 psz_expected, psz_real );
169 close( p_sys->i_frontend_handle );
174 else /* no frontend probing is done so use default border values. */
176 msg_Dbg( p_access, "using default values for frontend info" );
178 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
179 p_frontend->info.type = FE_QPSK;
180 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
181 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
182 p_frontend->info.type = FE_QPSK;
183 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
184 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
185 p_frontend->info.type = FE_QAM;
186 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
187 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
188 p_frontend->info.type = FE_OFDM;
194 /*****************************************************************************
195 * FrontendClose : Close the frontend
196 *****************************************************************************/
197 void E_(FrontendClose)( access_t *p_access )
199 access_sys_t *p_sys = p_access->p_sys;
201 if( p_sys->p_frontend )
203 close( p_sys->i_frontend_handle );
204 free( p_sys->p_frontend );
206 p_sys->p_frontend = NULL;
210 /*****************************************************************************
211 * FrontendSet : Tune !
212 *****************************************************************************/
213 int E_(FrontendSet)( access_t *p_access )
215 access_sys_t *p_sys = p_access->p_sys;
217 switch( p_sys->p_frontend->info.type )
221 if( FrontendSetQPSK( p_access ) < 0 )
223 msg_Err( p_access, "DVB-S: tuning failed" );
230 if( FrontendSetQAM( p_access ) < 0 )
232 msg_Err( p_access, "DVB-C: tuning failed" );
239 if( FrontendSetOFDM( p_access ) < 0 )
241 msg_Err( p_access, "DVB-T: tuning failed" );
247 msg_Err( p_access, "Could not determine frontend type on %s",
248 p_sys->p_frontend->info.name );
251 p_sys->p_frontend->i_last_status = 0;
252 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
256 /*****************************************************************************
257 * FrontendPoll : Poll for frontend events
258 *****************************************************************************/
259 void E_(FrontendPoll)( access_t *p_access )
261 access_sys_t *p_sys = p_access->p_sys;
262 frontend_t * p_frontend = p_sys->p_frontend;
263 struct dvb_frontend_event event;
264 fe_status_t i_status, i_diff;
268 int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
272 if( errno == EWOULDBLOCK )
275 msg_Err( p_access, "reading frontend status failed (%d) %s",
276 i_ret, strerror(errno) );
280 i_status = event.status;
281 i_diff = i_status ^ p_frontend->i_last_status;
282 p_frontend->i_last_status = i_status;
286 if ( i_diff & (x) ) \
288 if ( i_status & (x) )
291 IF_UP( FE_HAS_SIGNAL )
292 msg_Dbg( p_access, "frontend has acquired signal" );
294 msg_Dbg( p_access, "frontend has lost signal" );
296 IF_UP( FE_HAS_CARRIER )
297 msg_Dbg( p_access, "frontend has acquired carrier" );
299 msg_Dbg( p_access, "frontend has lost carrier" );
301 IF_UP( FE_HAS_VITERBI )
302 msg_Dbg( p_access, "frontend has acquired stable FEC" );
304 msg_Dbg( p_access, "frontend has lost FEC" );
307 msg_Dbg( p_access, "frontend has acquired sync" );
309 msg_Dbg( p_access, "frontend has lost sync" );
314 msg_Dbg( p_access, "frontend has acquired lock" );
315 p_sys->i_frontend_timeout = 0;
317 /* Read some statistics */
318 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
319 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
320 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
321 msg_Dbg( p_access, "- Signal strength: %d", i_value );
322 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
323 msg_Dbg( p_access, "- SNR: %d", i_value );
327 msg_Dbg( p_access, "frontend has lost lock" );
328 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
333 /* The frontend was reinited. */
334 msg_Warn( p_access, "reiniting frontend");
335 E_(FrontendSet)( p_access );
340 /*****************************************************************************
341 * FrontendInfo : Return information about given frontend
342 *****************************************************************************/
343 static int FrontendInfo( access_t *p_access )
345 access_sys_t *p_sys = p_access->p_sys;
346 frontend_t *p_frontend = p_sys->p_frontend;
349 /* Determine type of frontend */
350 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
351 &p_frontend->info )) < 0 )
353 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
358 /* Print out frontend capabilities. */
359 msg_Dbg(p_access, "Frontend Info:" );
360 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
361 switch( p_frontend->info.type )
364 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
367 msg_Dbg( p_access, " type = QAM (DVB-C)" );
370 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
372 #if 0 /* DVB_API_VERSION == 3 */
374 msg_Dbg(p_access, " type = MEMORY" );
377 msg_Dbg(p_access, " type = NETWORK" );
381 msg_Err( p_access, " unknown frontend type (%d)",
382 p_frontend->info.type );
385 msg_Dbg(p_access, " frequency_min = %u (kHz)",
386 p_frontend->info.frequency_min);
387 msg_Dbg(p_access, " frequency_max = %u (kHz)",
388 p_frontend->info.frequency_max);
389 msg_Dbg(p_access, " frequency_stepsize = %u",
390 p_frontend->info.frequency_stepsize);
391 msg_Dbg(p_access, " frequency_tolerance = %u",
392 p_frontend->info.frequency_tolerance);
393 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
394 p_frontend->info.symbol_rate_min);
395 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
396 p_frontend->info.symbol_rate_max);
397 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
398 p_frontend->info.symbol_rate_tolerance);
399 msg_Dbg(p_access, " notifier_delay (ms) = %u",
400 p_frontend->info.notifier_delay );
402 msg_Dbg(p_access, "Frontend Info capability list:");
403 if( p_frontend->info.caps & FE_IS_STUPID)
404 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
405 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
406 msg_Dbg(p_access, " inversion auto");
407 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
408 msg_Dbg(p_access, " forward error correction 1/2");
409 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
410 msg_Dbg(p_access, " forward error correction 2/3");
411 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
412 msg_Dbg(p_access, " forward error correction 3/4");
413 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
414 msg_Dbg(p_access, " forward error correction 4/5");
415 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
416 msg_Dbg(p_access, " forward error correction 5/6");
417 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
418 msg_Dbg(p_access, " forward error correction 6/7");
419 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
420 msg_Dbg(p_access, " forward error correction 7/8");
421 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
422 msg_Dbg(p_access, " forward error correction 8/9");
423 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
424 msg_Dbg(p_access, " forward error correction auto");
425 if( p_frontend->info.caps & FE_CAN_QPSK)
426 msg_Dbg(p_access, " card can do QPSK");
427 if( p_frontend->info.caps & FE_CAN_QAM_16)
428 msg_Dbg(p_access, " card can do QAM 16");
429 if( p_frontend->info.caps & FE_CAN_QAM_32)
430 msg_Dbg(p_access, " card can do QAM 32");
431 if( p_frontend->info.caps & FE_CAN_QAM_64)
432 msg_Dbg(p_access, " card can do QAM 64");
433 if( p_frontend->info.caps & FE_CAN_QAM_128)
434 msg_Dbg(p_access, " card can do QAM 128");
435 if( p_frontend->info.caps & FE_CAN_QAM_256)
436 msg_Dbg(p_access, " card can do QAM 256");
437 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
438 msg_Dbg(p_access, " card can do QAM auto");
439 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
440 msg_Dbg(p_access, " transmission mode auto");
441 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
442 msg_Dbg(p_access, " bandwidth mode auto");
443 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
444 msg_Dbg(p_access, " guard interval mode auto");
445 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
446 msg_Dbg(p_access, " hierarchy mode auto");
447 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
448 msg_Dbg(p_access, " card can mute TS");
449 if( p_frontend->info.caps & FE_CAN_RECOVER)
450 msg_Dbg(p_access, " card can recover from a cable unplug");
451 msg_Dbg(p_access, "End of capability list");
456 /*****************************************************************************
457 * Decoding the DVB parameters (common)
458 *****************************************************************************/
459 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
462 fe_spectral_inversion_t fe_inversion = 0;
464 var_Get( p_access, "dvb-inversion", &val );
465 msg_Dbg( p_access, "using inversion=%d", val.i_int );
469 case 0: fe_inversion = INVERSION_OFF; break;
470 case 1: fe_inversion = INVERSION_ON; break;
471 case 2: fe_inversion = INVERSION_AUTO; break;
473 msg_Dbg( p_access, "dvb has inversion not set, using auto");
474 fe_inversion = INVERSION_AUTO;
480 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
482 fe_code_rate_t fe_fec = FEC_NONE;
484 msg_Dbg( p_access, "using fec=%d", i_val );
488 case 0: fe_fec = FEC_NONE; break;
489 case 1: fe_fec = FEC_1_2; break;
490 case 2: fe_fec = FEC_2_3; break;
491 case 3: fe_fec = FEC_3_4; break;
492 case 4: fe_fec = FEC_4_5; break;
493 case 5: fe_fec = FEC_5_6; break;
494 case 6: fe_fec = FEC_6_7; break;
495 case 7: fe_fec = FEC_7_8; break;
496 case 8: fe_fec = FEC_8_9; break;
497 case 9: fe_fec = FEC_AUTO; break;
501 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
507 static fe_modulation_t DecodeModulation( access_t *p_access )
510 fe_modulation_t fe_modulation = 0;
512 var_Get( p_access, "dvb-modulation", &val );
516 case -1: fe_modulation = QPSK; break;
517 case 0: fe_modulation = QAM_AUTO; break;
518 case 16: fe_modulation = QAM_16; break;
519 case 32: fe_modulation = QAM_32; break;
520 case 64: fe_modulation = QAM_64; break;
521 case 128: fe_modulation = QAM_128; break;
522 case 256: fe_modulation = QAM_256; break;
524 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
525 fe_modulation = QAM_AUTO;
528 return fe_modulation;
531 /*****************************************************************************
532 * FrontendSetQPSK : controls the FE device
533 *****************************************************************************/
534 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
537 fe_sec_voltage_t fe_voltage;
539 var_Get( p_access, "dvb-voltage", &val );
540 msg_Dbg( p_access, "using voltage=%d", val.i_int );
544 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
545 case 13: fe_voltage = SEC_VOLTAGE_13; break;
546 case 18: fe_voltage = SEC_VOLTAGE_18; break;
548 fe_voltage = SEC_VOLTAGE_OFF;
549 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
555 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
558 fe_sec_tone_mode_t fe_tone;
560 var_Get( p_access, "dvb-tone", &val );
561 msg_Dbg( p_access, "using tone=%d", val.i_int );
565 case 0: fe_tone = SEC_TONE_OFF; break;
566 case 1: fe_tone = SEC_TONE_ON; break;
568 fe_tone = SEC_TONE_OFF;
569 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
577 struct dvb_diseqc_master_cmd cmd;
581 static int DoDiseqc( access_t *p_access )
583 access_sys_t *p_sys = p_access->p_sys;
585 int i_frequency, i_lnb_slof;
586 fe_sec_voltage_t fe_voltage;
587 fe_sec_tone_mode_t fe_tone;
590 var_Get( p_access, "dvb-frequency", &val );
591 i_frequency = val.i_int;
592 var_Get( p_access, "dvb-lnb-slof", &val );
593 i_lnb_slof = val.i_int;
595 var_Get( p_access, "dvb-tone", &val );
596 if( val.i_int == -1 /* auto */ )
598 if( i_frequency >= i_lnb_slof )
602 var_Set( p_access, "dvb-tone", val );
605 fe_voltage = DecodeVoltage( p_access );
606 fe_tone = DecodeTone( p_access );
608 /* Switch off continuous tone. */
609 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
611 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
612 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
617 /* Configure LNB voltage. */
618 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
620 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
621 fe_voltage, i_err, strerror(errno) );
625 var_Get( p_access, "dvb-high-voltage", &val );
626 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
627 val.b_bool )) < 0 && val.b_bool )
630 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
631 val.b_bool, i_err, strerror(errno) );
634 /* Wait for at least 15 ms. */
637 var_Get( p_access, "dvb-satno", &val );
638 if( val.i_int > 0 && val.i_int < 5 )
640 /* digital satellite equipment control,
641 * specification is available from http://www.eutelsat.com/
644 /* 1.x compatible equipment */
645 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
647 /* param: high nibble: reset bits, low nibble set bits,
648 * bits are: option, position, polarization, band
650 cmd.cmd.msg[3] = 0xf0 /* reset bits */
651 | (((val.i_int - 1) * 4) & 0xc)
652 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
653 | (fe_tone == SEC_TONE_ON ? 1 : 0);
655 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
658 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
659 i_err, strerror(errno) );
663 msleep(15000 + cmd.wait * 1000);
665 /* A or B simple diseqc ("diseqc-compatible") */
666 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
667 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
669 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
670 i_err, strerror(errno) );
677 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
679 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
680 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
689 static int FrontendSetQPSK( access_t *p_access )
691 access_sys_t *p_sys = p_access->p_sys;
692 struct dvb_frontend_parameters fep;
695 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
697 /* Prepare the fep structure */
698 var_Get( p_access, "dvb-frequency", &val );
699 i_frequency = val.i_int;
701 var_Get( p_access, "dvb-lnb-lof1", &val );
702 if ( val.i_int == 0 )
704 /* Automatic mode. */
705 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
707 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
710 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
712 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
713 i_lnb_lof1 = 3650000;
715 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
717 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
719 i_lnb_lof1 = 5150000;
721 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
723 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
725 i_lnb_lof1 = 5950000;
727 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
729 msg_Dbg( p_access, "frequency %d is in Ku-band",
731 i_lnb_lof1 = 9750000;
732 i_lnb_lof2 = 10600000;
733 i_lnb_slof = 11700000;
737 msg_Err( p_access, "frequency %d is out of any known band",
739 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
740 "oscillator frequency" );
743 val.i_int = i_lnb_lof1;
744 var_Set( p_access, "dvb-lnb-lof1", val );
745 val.i_int = i_lnb_lof2;
746 var_Set( p_access, "dvb-lnb-lof2", val );
747 val.i_int = i_lnb_slof;
748 var_Set( p_access, "dvb-lnb-slof", val );
752 i_lnb_lof1 = val.i_int;
753 var_Get( p_access, "dvb-lnb-lof2", &val );
754 i_lnb_lof2 = val.i_int;
755 var_Get( p_access, "dvb-lnb-slof", &val );
756 i_lnb_slof = val.i_int;
759 if( i_lnb_slof && i_frequency >= i_lnb_slof )
761 i_frequency -= i_lnb_lof2;
765 i_frequency -= i_lnb_lof1;
767 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
769 fep.inversion = DecodeInversion( p_access );
771 var_Get( p_access, "dvb-srate", &val );
772 fep.u.qpsk.symbol_rate = val.i_int;
774 var_Get( p_access, "dvb-fec", &val );
775 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
777 if( DoDiseqc( p_access ) < 0 )
782 /* Empty the event queue */
785 struct dvb_frontend_event event;
786 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
787 && errno == EWOULDBLOCK )
791 /* Now send it all to the frontend device */
792 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
794 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
802 /*****************************************************************************
803 * FrontendSetQAM : controls the FE device
804 *****************************************************************************/
805 static int FrontendSetQAM( access_t *p_access )
807 access_sys_t *p_sys = p_access->p_sys;
808 struct dvb_frontend_parameters fep;
812 /* Prepare the fep structure */
814 var_Get( p_access, "dvb-frequency", &val );
815 fep.frequency = val.i_int;
817 fep.inversion = DecodeInversion( p_access );
819 var_Get( p_access, "dvb-srate", &val );
820 fep.u.qam.symbol_rate = val.i_int;
822 var_Get( p_access, "dvb-fec", &val );
823 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
825 fep.u.qam.modulation = DecodeModulation( p_access );
827 /* Empty the event queue */
830 struct dvb_frontend_event event;
831 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
832 && errno == EWOULDBLOCK )
836 /* Now send it all to the frontend device */
837 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
839 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
847 /*****************************************************************************
848 * FrontendSetOFDM : controls the FE device
849 *****************************************************************************/
850 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
853 fe_bandwidth_t fe_bandwidth = 0;
855 var_Get( p_access, "dvb-bandwidth", &val );
856 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
860 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
861 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
862 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
863 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
865 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
866 fe_bandwidth = BANDWIDTH_AUTO;
872 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
875 fe_transmit_mode_t fe_transmission = 0;
877 var_Get( p_access, "dvb-transmission", &val );
878 msg_Dbg( p_access, "using transmission=%d", val.i_int );
882 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
883 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
884 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
886 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
887 fe_transmission = TRANSMISSION_MODE_AUTO;
890 return fe_transmission;
893 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
896 fe_guard_interval_t fe_guard = 0;
898 var_Get( p_access, "dvb-guard", &val );
899 msg_Dbg( p_access, "using guard=%d", val.i_int );
903 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
904 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
905 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
906 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
907 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
909 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
910 fe_guard = GUARD_INTERVAL_AUTO;
916 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
919 fe_hierarchy_t fe_hierarchy = 0;
921 var_Get( p_access, "dvb-hierarchy", &val );
922 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
926 case -1: fe_hierarchy = HIERARCHY_NONE; break;
927 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
928 case 1: fe_hierarchy = HIERARCHY_1; break;
929 case 2: fe_hierarchy = HIERARCHY_2; break;
930 case 4: fe_hierarchy = HIERARCHY_4; break;
932 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
933 fe_hierarchy = HIERARCHY_AUTO;
939 static int FrontendSetOFDM( access_t * p_access )
941 access_sys_t *p_sys = p_access->p_sys;
942 struct dvb_frontend_parameters fep;
946 /* Prepare the fep structure */
948 var_Get( p_access, "dvb-frequency", &val );
949 fep.frequency = val.i_int;
951 fep.inversion = DecodeInversion( p_access );
953 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
954 var_Get( p_access, "dvb-code-rate-hp", &val );
955 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
956 var_Get( p_access, "dvb-code-rate-lp", &val );
957 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
958 fep.u.ofdm.constellation = DecodeModulation( p_access );
959 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
960 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
961 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
963 /* Empty the event queue */
966 struct dvb_frontend_event event;
967 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
968 && errno == EWOULDBLOCK )
972 /* Now send it all to the frontend device */
973 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
975 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
988 /*****************************************************************************
989 * DMXSetFilter : controls the demux to add a filter
990 *****************************************************************************/
991 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
993 struct dmx_pes_filter_params s_filter_params;
995 unsigned int i_adapter, i_device;
999 var_Get( p_access, "dvb-adapter", &val );
1000 i_adapter = val.i_int;
1001 var_Get( p_access, "dvb-device", &val );
1002 i_device = val.i_int;
1004 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1005 >= (int)sizeof(dmx) )
1007 msg_Err( p_access, "snprintf() truncated string for DMX" );
1008 dmx[sizeof(dmx) - 1] = '\0';
1011 msg_Dbg( p_access, "Opening device %s", dmx );
1012 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1014 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
1016 return VLC_EGENERIC;
1019 /* We fill the DEMUX structure : */
1020 s_filter_params.pid = i_pid;
1021 s_filter_params.input = DMX_IN_FRONTEND;
1022 s_filter_params.output = DMX_OUT_TS_TAP;
1023 s_filter_params.flags = DMX_IMMEDIATE_START;
1026 { /* First device */
1028 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1029 s_filter_params.pes_type = DMX_PES_VIDEO0;
1032 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1033 s_filter_params.pes_type = DMX_PES_AUDIO0;
1036 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1037 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1040 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1041 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1044 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1045 s_filter_params.pes_type = DMX_PES_PCR0;
1049 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1050 s_filter_params.pes_type = DMX_PES_VIDEO1;
1053 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1054 s_filter_params.pes_type = DMX_PES_AUDIO1;
1057 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1058 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1061 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1062 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1065 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1066 s_filter_params.pes_type = DMX_PES_PCR1;
1070 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1071 s_filter_params.pes_type = DMX_PES_VIDEO2;
1074 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1075 s_filter_params.pes_type = DMX_PES_AUDIO2;
1078 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1079 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1082 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1083 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1086 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1087 s_filter_params.pes_type = DMX_PES_PCR2;
1091 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1092 s_filter_params.pes_type = DMX_PES_VIDEO3;
1095 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1096 s_filter_params.pes_type = DMX_PES_AUDIO3;
1099 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1100 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1103 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1104 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1107 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1108 s_filter_params.pes_type = DMX_PES_PCR3;
1110 /* Usually used by Nova cards */
1113 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1114 s_filter_params.pes_type = DMX_PES_OTHER;
1118 /* We then give the order to the device : */
1119 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1121 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1123 return VLC_EGENERIC;
1128 /*****************************************************************************
1129 * DMXUnsetFilter : removes a filter
1130 *****************************************************************************/
1131 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1135 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1137 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1138 i_ret, strerror(errno) );
1142 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1152 /*****************************************************************************
1154 *****************************************************************************/
1155 int E_(DVROpen)( access_t * p_access )
1157 access_sys_t *p_sys = p_access->p_sys;
1158 unsigned int i_adapter, i_device;
1162 var_Get( p_access, "dvb-adapter", &val );
1163 i_adapter = val.i_int;
1164 var_Get( p_access, "dvb-device", &val );
1165 i_device = val.i_int;
1167 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1168 >= (int)sizeof(dvr) )
1170 msg_Err( p_access, "snprintf() truncated string for DVR" );
1171 dvr[sizeof(dvr) - 1] = '\0';
1174 msg_Dbg( p_access, "Opening device %s", dvr );
1175 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1177 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1179 return VLC_EGENERIC;
1182 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1184 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%s)",
1191 /*****************************************************************************
1193 *****************************************************************************/
1194 void E_(DVRClose)( access_t * p_access )
1196 access_sys_t *p_sys = p_access->p_sys;
1198 close( p_sys->i_handle );
1206 /*****************************************************************************
1208 *****************************************************************************/
1209 int E_(CAMOpen)( access_t *p_access )
1211 access_sys_t *p_sys = p_access->p_sys;
1213 int i_adapter, i_device, i_slot;
1215 struct ca_slot_info info;
1217 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1218 i_device = var_GetInteger( p_access, "dvb-device" );
1220 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1222 msg_Err( p_access, "snprintf() truncated string for CA" );
1223 ca[sizeof(ca) - 1] = '\0';
1225 memset( &caps, 0, sizeof( ca_caps_t ));
1227 msg_Dbg( p_access, "Opening device %s", ca );
1228 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1230 msg_Warn( p_access, "CAMInit: opening CAM device failed (%s)",
1232 p_sys->i_ca_handle = 0;
1233 return VLC_EGENERIC;
1236 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1238 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1239 close( p_sys->i_ca_handle );
1240 p_sys->i_ca_handle = 0;
1241 return VLC_EGENERIC;
1244 /* Output CA capabilities */
1245 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1246 caps.slot_num == 1 ? "slot" : "slots" );
1247 if ( caps.slot_type & CA_CI )
1248 msg_Dbg( p_access, "CAMInit: CI high level interface type (not supported)" );
1249 if ( caps.slot_type & CA_CI_LINK )
1250 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1251 if ( caps.slot_type & CA_CI_PHYS )
1252 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1253 if ( caps.slot_type & CA_DESCR )
1254 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1255 if ( caps.slot_type & CA_SC )
1256 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1258 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1259 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1260 if ( caps.descr_type & CA_ECD )
1261 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1262 if ( caps.descr_type & CA_NDS )
1263 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1264 if ( caps.descr_type & CA_DSS )
1265 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1267 if ( caps.slot_num == 0 )
1269 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1270 close( p_sys->i_ca_handle );
1271 p_sys->i_ca_handle = 0;
1272 return VLC_EGENERIC;
1275 p_sys->i_ca_type = caps.slot_type;
1276 if( caps.slot_type != CA_CI_LINK &&
1277 caps.slot_type != CA_CI )
1279 msg_Err( p_access, "CAMInit: incompatible CAM module" );
1280 close( p_sys->i_ca_handle );
1281 p_sys->i_ca_handle = 0;
1282 return VLC_EGENERIC;
1284 if( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &info ) < 0 )
1286 msg_Err( p_access, "CAMInit: Couldn't get slot info" );
1287 close( p_sys->i_ca_handle );
1288 p_sys->i_ca_handle = 0;
1289 return VLC_EGENERIC;
1291 if( info.flags == 0 )
1293 msg_Err( p_access, "CAMInit: No CAM inserted" );
1294 close( p_sys->i_ca_handle );
1295 p_sys->i_ca_handle = 0;
1296 return VLC_EGENERIC;
1299 p_sys->i_nb_slots = caps.slot_num;
1300 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1301 #if(RESET_CAM_SLOTS)
1302 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1304 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1306 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1311 p_sys->i_ca_timeout = 100000;
1312 /* Wait a bit otherwise it doesn't initialize properly... */
1315 return E_(en50221_Init)( p_access );
1318 /*****************************************************************************
1320 *****************************************************************************/
1321 int E_(CAMPoll)( access_t * p_access )
1323 access_sys_t *p_sys = p_access->p_sys;
1324 int i_ret = VLC_EGENERIC;
1326 if ( p_sys->i_ca_handle == 0 )
1328 return VLC_EGENERIC;
1331 switch( p_sys->i_ca_type )
1334 i_ret = E_(en50221_Poll)( p_access );
1337 i_ret = VLC_SUCCESS;
1340 msg_Err( p_access, "CAMPoll: This should not happen" );
1347 /*****************************************************************************
1349 *****************************************************************************/
1350 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1352 access_sys_t *p_sys = p_access->p_sys;
1354 if ( p_sys->i_ca_handle == 0 )
1356 return VLC_EGENERIC;
1359 E_(en50221_SetCAPMT)( p_access, p_pmt );
1364 /*****************************************************************************
1366 *****************************************************************************/
1367 void E_(CAMClose)( access_t * p_access )
1369 access_sys_t *p_sys = p_access->p_sys;
1371 E_(en50221_End)( p_access );
1373 if ( p_sys->i_ca_handle )
1375 close( p_sys->i_ca_handle );