1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
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 /* Local prototypes */
76 static int FrontendInfo( access_t * );
77 static int FrontendSetQPSK( access_t * );
78 static int FrontendSetQAM( access_t * );
79 static int FrontendSetOFDM( access_t * );
81 /*****************************************************************************
82 * FrontendOpen : Determine frontend device information and capabilities
83 *****************************************************************************/
84 int E_(FrontendOpen)( access_t *p_access )
86 access_sys_t *p_sys = p_access->p_sys;
87 frontend_t * p_frontend;
88 unsigned int i_adapter, i_device;
92 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
93 i_device = var_GetInteger( p_access, "dvb-device" );
94 b_probe = var_GetBool( p_access, "dvb-probe" );
96 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
98 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
99 frontend[sizeof(frontend) - 1] = '\0';
102 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
104 msg_Dbg( p_access, "Opening device %s", frontend );
105 if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
107 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
115 char * psz_expected = NULL;
118 if( FrontendInfo( p_access ) < 0 )
120 close( p_sys->i_frontend_handle );
125 switch( p_frontend->info.type )
137 psz_real = "unknown";
141 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
142 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
143 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
144 (p_frontend->info.type != FE_QPSK) )
146 psz_expected = "DVB-S";
148 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
149 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
150 (p_frontend->info.type != FE_QAM) )
152 psz_expected = "DVB-C";
154 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
155 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
156 (p_frontend->info.type != FE_OFDM) )
158 psz_expected = "DVB-T";
161 if( psz_expected != NULL )
163 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
164 psz_expected, psz_real );
165 close( p_sys->i_frontend_handle );
170 else /* no frontend probing is done so use default border values. */
172 msg_Dbg( p_access, "using default values for frontend info" );
174 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
175 p_frontend->info.type = FE_QPSK;
176 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
177 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
178 p_frontend->info.type = FE_QPSK;
179 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
180 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
181 p_frontend->info.type = FE_QAM;
182 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
183 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
184 p_frontend->info.type = FE_OFDM;
190 /*****************************************************************************
191 * FrontendClose : Close the frontend
192 *****************************************************************************/
193 void E_(FrontendClose)( access_t *p_access )
195 access_sys_t *p_sys = p_access->p_sys;
197 if( p_sys->p_frontend )
199 close( p_sys->i_frontend_handle );
200 free( p_sys->p_frontend );
202 p_sys->p_frontend = NULL;
206 /*****************************************************************************
207 * FrontendSet : Tune !
208 *****************************************************************************/
209 int E_(FrontendSet)( access_t *p_access )
211 access_sys_t *p_sys = p_access->p_sys;
213 switch( p_sys->p_frontend->info.type )
217 if( FrontendSetQPSK( p_access ) < 0 )
219 msg_Err( p_access, "DVB-S: tuning failed" );
226 if( FrontendSetQAM( p_access ) < 0 )
228 msg_Err( p_access, "DVB-C: tuning failed" );
235 if( FrontendSetOFDM( p_access ) < 0 )
237 msg_Err( p_access, "DVB-T: tuning failed" );
243 msg_Err( p_access, "Could not determine frontend type on %s",
244 p_sys->p_frontend->info.name );
247 p_sys->p_frontend->i_last_status = 0;
251 /*****************************************************************************
252 * FrontendPoll : Poll for frontend events
253 *****************************************************************************/
254 void E_(FrontendPoll)( access_t *p_access )
256 access_sys_t *p_sys = p_access->p_sys;
257 frontend_t * p_frontend = p_sys->p_frontend;
259 struct dvb_frontend_event event;
260 fe_status_t i_status, i_diff;
262 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT,
265 msg_Err( p_access, "reading frontend status failed (%d) %s",
266 i_ret, strerror(errno) );
270 i_status = event.status;
271 i_diff = i_status ^ p_frontend->i_last_status;
272 p_frontend->i_last_status = i_status;
276 if ( i_diff & (x) ) \
278 if ( i_status & (x) )
281 IF_UP( FE_HAS_SIGNAL )
282 msg_Dbg( p_access, "frontend has acquired signal" );
284 msg_Dbg( p_access, "frontend has lost signal" );
286 IF_UP( FE_HAS_CARRIER )
287 msg_Dbg( p_access, "frontend has acquired carrier" );
289 msg_Dbg( p_access, "frontend has lost carrier" );
291 IF_UP( FE_HAS_VITERBI )
292 msg_Dbg( p_access, "frontend has acquired stable FEC" );
294 msg_Dbg( p_access, "frontend has lost FEC" );
297 msg_Dbg( p_access, "frontend has acquired sync" );
299 msg_Dbg( p_access, "frontend has lost sync" );
304 msg_Dbg( p_access, "frontend has acquired lock" );
306 /* Read some statistics */
307 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
308 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
309 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
310 msg_Dbg( p_access, "- Signal strength: %d", i_value );
311 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
312 msg_Dbg( p_access, "- SNR: %d", i_value );
316 msg_Dbg( p_access, "frontend has lost lock" );
321 /* The frontend was reinited. */
322 msg_Warn( p_access, "reiniting frontend");
323 E_(FrontendSet)( p_access );
327 /*****************************************************************************
328 * FrontendInfo : Return information about given frontend
329 *****************************************************************************/
330 static int FrontendInfo( access_t *p_access )
332 access_sys_t *p_sys = p_access->p_sys;
333 frontend_t *p_frontend = p_sys->p_frontend;
336 /* Determine type of frontend */
337 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
338 &p_frontend->info )) < 0 )
340 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
345 /* Print out frontend capabilities. */
346 msg_Dbg(p_access, "Frontend Info:" );
347 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
348 switch( p_frontend->info.type )
351 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
354 msg_Dbg( p_access, " type = QAM (DVB-C)" );
357 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
359 #if 0 /* DVB_API_VERSION == 3 */
361 msg_Dbg(p_access, " type = MEMORY" );
364 msg_Dbg(p_access, " type = NETWORK" );
368 msg_Err( p_access, " unknown frontend type (%d)",
369 p_frontend->info.type );
372 msg_Dbg(p_access, " frequency_min = %u (kHz)",
373 p_frontend->info.frequency_min);
374 msg_Dbg(p_access, " frequency_max = %u (kHz)",
375 p_frontend->info.frequency_max);
376 msg_Dbg(p_access, " frequency_stepsize = %u",
377 p_frontend->info.frequency_stepsize);
378 msg_Dbg(p_access, " frequency_tolerance = %u",
379 p_frontend->info.frequency_tolerance);
380 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
381 p_frontend->info.symbol_rate_min);
382 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
383 p_frontend->info.symbol_rate_max);
384 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
385 p_frontend->info.symbol_rate_tolerance);
386 msg_Dbg(p_access, " notifier_delay (ms) = %u",
387 p_frontend->info.notifier_delay );
389 msg_Dbg(p_access, "Frontend Info capability list:");
390 if( p_frontend->info.caps & FE_IS_STUPID)
391 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
392 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
393 msg_Dbg(p_access, " inversion auto");
394 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
395 msg_Dbg(p_access, " forward error correction 1/2");
396 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
397 msg_Dbg(p_access, " forward error correction 2/3");
398 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
399 msg_Dbg(p_access, " forward error correction 3/4");
400 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
401 msg_Dbg(p_access, " forward error correction 4/5");
402 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
403 msg_Dbg(p_access, " forward error correction 5/6");
404 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
405 msg_Dbg(p_access, " forward error correction 6/7");
406 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
407 msg_Dbg(p_access, " forward error correction 7/8");
408 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
409 msg_Dbg(p_access, " forward error correction 8/9");
410 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
411 msg_Dbg(p_access, " forward error correction auto");
412 if( p_frontend->info.caps & FE_CAN_QPSK)
413 msg_Dbg(p_access, " card can do QPSK");
414 if( p_frontend->info.caps & FE_CAN_QAM_16)
415 msg_Dbg(p_access, " card can do QAM 16");
416 if( p_frontend->info.caps & FE_CAN_QAM_32)
417 msg_Dbg(p_access, " card can do QAM 32");
418 if( p_frontend->info.caps & FE_CAN_QAM_64)
419 msg_Dbg(p_access, " card can do QAM 64");
420 if( p_frontend->info.caps & FE_CAN_QAM_128)
421 msg_Dbg(p_access, " card can do QAM 128");
422 if( p_frontend->info.caps & FE_CAN_QAM_256)
423 msg_Dbg(p_access, " card can do QAM 256");
424 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
425 msg_Dbg(p_access, " card can do QAM auto");
426 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
427 msg_Dbg(p_access, " transmission mode auto");
428 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
429 msg_Dbg(p_access, " bandwidth mode auto");
430 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
431 msg_Dbg(p_access, " guard interval mode auto");
432 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
433 msg_Dbg(p_access, " hierarchy mode auto");
434 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
435 msg_Dbg(p_access, " card can mute TS");
436 if( p_frontend->info.caps & FE_CAN_RECOVER)
437 msg_Dbg(p_access, " card can recover from a cable unplug");
438 msg_Dbg(p_access, "End of capability list");
443 /*****************************************************************************
444 * Decoding the DVB parameters (common)
445 *****************************************************************************/
446 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
449 fe_spectral_inversion_t fe_inversion = 0;
451 var_Get( p_access, "dvb-inversion", &val );
452 msg_Dbg( p_access, "using inversion=%d", val.i_int );
456 case 0: fe_inversion = INVERSION_OFF; break;
457 case 1: fe_inversion = INVERSION_ON; break;
458 case 2: fe_inversion = INVERSION_AUTO; break;
460 msg_Dbg( p_access, "dvb has inversion not set, using auto");
461 fe_inversion = INVERSION_AUTO;
467 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
469 fe_code_rate_t fe_fec = FEC_NONE;
471 msg_Dbg( p_access, "using fec=%d", i_val );
475 case 0: fe_fec = FEC_NONE; break;
476 case 1: fe_fec = FEC_1_2; break;
477 case 2: fe_fec = FEC_2_3; break;
478 case 3: fe_fec = FEC_3_4; break;
479 case 4: fe_fec = FEC_4_5; break;
480 case 5: fe_fec = FEC_5_6; break;
481 case 6: fe_fec = FEC_6_7; break;
482 case 7: fe_fec = FEC_7_8; break;
483 case 8: fe_fec = FEC_8_9; break;
484 case 9: fe_fec = FEC_AUTO; break;
488 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
494 static fe_modulation_t DecodeModulation( access_t *p_access )
497 fe_modulation_t fe_modulation = 0;
499 var_Get( p_access, "dvb-modulation", &val );
503 case -1: fe_modulation = QPSK; break;
504 case 0: fe_modulation = QAM_AUTO; break;
505 case 16: fe_modulation = QAM_16; break;
506 case 32: fe_modulation = QAM_32; break;
507 case 64: fe_modulation = QAM_64; break;
508 case 128: fe_modulation = QAM_128; break;
509 case 256: fe_modulation = QAM_256; break;
511 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
512 fe_modulation = QAM_AUTO;
515 return fe_modulation;
518 /*****************************************************************************
519 * FrontendSetQPSK : controls the FE device
520 *****************************************************************************/
521 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
524 fe_sec_voltage_t fe_voltage;
526 var_Get( p_access, "dvb-voltage", &val );
527 msg_Dbg( p_access, "using voltage=%d", val.i_int );
531 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
532 case 13: fe_voltage = SEC_VOLTAGE_13; break;
533 case 18: fe_voltage = SEC_VOLTAGE_18; break;
535 fe_voltage = SEC_VOLTAGE_OFF;
536 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
542 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
545 fe_sec_tone_mode_t fe_tone;
547 var_Get( p_access, "dvb-tone", &val );
548 msg_Dbg( p_access, "using tone=%d", val.i_int );
552 case 0: fe_tone = SEC_TONE_OFF; break;
553 case 1: fe_tone = SEC_TONE_ON; break;
555 fe_tone = SEC_TONE_OFF;
556 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
564 struct dvb_diseqc_master_cmd cmd;
568 static int DoDiseqc( access_t *p_access )
570 access_sys_t *p_sys = p_access->p_sys;
572 int i_frequency, i_lnb_slof;
573 fe_sec_voltage_t fe_voltage;
574 fe_sec_tone_mode_t fe_tone;
577 var_Get( p_access, "dvb-frequency", &val );
578 i_frequency = val.i_int;
579 var_Get( p_access, "dvb-lnb-slof", &val );
580 i_lnb_slof = val.i_int;
582 var_Get( p_access, "dvb-tone", &val );
583 if( val.i_int == -1 /* auto */ )
585 if( i_frequency >= i_lnb_slof )
589 var_Set( p_access, "dvb-tone", val );
592 fe_voltage = DecodeVoltage( p_access );
593 fe_tone = DecodeTone( p_access );
595 /* Switch off continuous tone. */
596 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
598 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
599 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
604 /* Configure LNB voltage. */
605 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
607 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
608 fe_voltage, i_err, strerror(errno) );
612 var_Get( p_access, "dvb-high-voltage", &val );
613 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
614 val.b_bool )) < 0 && val.b_bool )
617 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
618 val.b_bool, i_err, strerror(errno) );
621 /* Wait for at least 15 ms. */
624 var_Get( p_access, "dvb-satno", &val );
625 if( val.i_int > 0 && val.i_int < 5 )
627 /* digital satellite equipment control,
628 * specification is available from http://www.eutelsat.com/
631 /* 1.x compatible equipment */
632 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
634 /* param: high nibble: reset bits, low nibble set bits,
635 * bits are: option, position, polarization, band
637 cmd.cmd.msg[3] = 0xf0 /* reset bits */
638 | (((val.i_int - 1) * 4) & 0xc)
639 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
640 | (fe_tone == SEC_TONE_ON ? 1 : 0);
642 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
645 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
646 i_err, strerror(errno) );
650 msleep(15000 + cmd.wait * 1000);
652 /* A or B simple diseqc ("diseqc-compatible") */
653 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
654 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
656 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
657 i_err, strerror(errno) );
664 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
666 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
667 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
676 static int FrontendSetQPSK( access_t *p_access )
678 access_sys_t *p_sys = p_access->p_sys;
679 struct dvb_frontend_parameters fep;
682 int i_frequency, i_lnb_slof;
684 /* Prepare the fep structure */
685 var_Get( p_access, "dvb-frequency", &val );
686 i_frequency = val.i_int;
687 var_Get( p_access, "dvb-lnb-slof", &val );
688 i_lnb_slof = val.i_int;
690 if( i_frequency >= i_lnb_slof )
691 var_Get( p_access, "dvb-lnb-lof2", &val );
693 var_Get( p_access, "dvb-lnb-lof1", &val );
694 fep.frequency = i_frequency - val.i_int;
696 fep.inversion = DecodeInversion( p_access );
698 var_Get( p_access, "dvb-srate", &val );
699 fep.u.qpsk.symbol_rate = val.i_int;
701 var_Get( p_access, "dvb-fec", &val );
702 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
704 if( DoDiseqc( p_access ) < 0 )
709 /* Empty the event queue */
712 struct dvb_frontend_event event;
713 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
714 && errno == EWOULDBLOCK )
718 /* Now send it all to the frontend device */
719 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
721 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
729 /*****************************************************************************
730 * FrontendSetQAM : controls the FE device
731 *****************************************************************************/
732 static int FrontendSetQAM( access_t *p_access )
734 access_sys_t *p_sys = p_access->p_sys;
735 struct dvb_frontend_parameters fep;
739 /* Prepare the fep structure */
741 var_Get( p_access, "dvb-frequency", &val );
742 fep.frequency = val.i_int;
744 fep.inversion = DecodeInversion( p_access );
746 var_Get( p_access, "dvb-srate", &val );
747 fep.u.qam.symbol_rate = val.i_int;
749 var_Get( p_access, "dvb-fec", &val );
750 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
752 fep.u.qam.modulation = DecodeModulation( p_access );
754 /* Empty the event queue */
757 struct dvb_frontend_event event;
758 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
759 && errno == EWOULDBLOCK )
763 /* Now send it all to the frontend device */
764 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
766 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
774 /*****************************************************************************
775 * FrontendSetOFDM : controls the FE device
776 *****************************************************************************/
777 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
780 fe_bandwidth_t fe_bandwidth = 0;
782 var_Get( p_access, "dvb-bandwidth", &val );
783 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
787 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
788 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
789 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
790 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
792 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
793 fe_bandwidth = BANDWIDTH_AUTO;
799 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
802 fe_transmit_mode_t fe_transmission = 0;
804 var_Get( p_access, "dvb-transmission", &val );
805 msg_Dbg( p_access, "using transmission=%d", val.i_int );
809 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
810 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
811 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
813 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
814 fe_transmission = TRANSMISSION_MODE_AUTO;
817 return fe_transmission;
820 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
823 fe_guard_interval_t fe_guard = 0;
825 var_Get( p_access, "dvb-guard", &val );
826 msg_Dbg( p_access, "using guard=%d", val.i_int );
830 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
831 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
832 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
833 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
834 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
836 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
837 fe_guard = GUARD_INTERVAL_AUTO;
843 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
846 fe_hierarchy_t fe_hierarchy = 0;
848 var_Get( p_access, "dvb-hierarchy", &val );
849 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
853 case -1: fe_hierarchy = HIERARCHY_NONE; break;
854 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
855 case 1: fe_hierarchy = HIERARCHY_1; break;
856 case 2: fe_hierarchy = HIERARCHY_2; break;
857 case 4: fe_hierarchy = HIERARCHY_4; break;
859 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
860 fe_hierarchy = HIERARCHY_AUTO;
866 static int FrontendSetOFDM( access_t * p_access )
868 access_sys_t *p_sys = p_access->p_sys;
869 struct dvb_frontend_parameters fep;
873 /* Prepare the fep structure */
875 var_Get( p_access, "dvb-frequency", &val );
876 fep.frequency = val.i_int;
878 fep.inversion = DecodeInversion( p_access );
880 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
881 var_Get( p_access, "dvb-code-rate-hp", &val );
882 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
883 var_Get( p_access, "dvb-code-rate-lp", &val );
884 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
885 fep.u.ofdm.constellation = DecodeModulation( p_access );
886 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
887 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
888 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
890 /* Empty the event queue */
893 struct dvb_frontend_event event;
894 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
895 && errno == EWOULDBLOCK )
899 /* Now send it all to the frontend device */
900 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
902 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
915 /*****************************************************************************
916 * DMXSetFilter : controls the demux to add a filter
917 *****************************************************************************/
918 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
920 struct dmx_pes_filter_params s_filter_params;
922 unsigned int i_adapter, i_device;
926 var_Get( p_access, "dvb-adapter", &val );
927 i_adapter = val.i_int;
928 var_Get( p_access, "dvb-device", &val );
929 i_device = val.i_int;
931 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
932 >= (int)sizeof(dmx) )
934 msg_Err( p_access, "snprintf() truncated string for DMX" );
935 dmx[sizeof(dmx) - 1] = '\0';
938 msg_Dbg( p_access, "Opening device %s", dmx );
939 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
941 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
946 /* We fill the DEMUX structure : */
947 s_filter_params.pid = i_pid;
948 s_filter_params.input = DMX_IN_FRONTEND;
949 s_filter_params.output = DMX_OUT_TS_TAP;
950 s_filter_params.flags = DMX_IMMEDIATE_START;
955 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
956 s_filter_params.pes_type = DMX_PES_VIDEO0;
959 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
960 s_filter_params.pes_type = DMX_PES_AUDIO0;
963 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
964 s_filter_params.pes_type = DMX_PES_TELETEXT0;
967 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
968 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
971 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
972 s_filter_params.pes_type = DMX_PES_PCR0;
976 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
977 s_filter_params.pes_type = DMX_PES_VIDEO1;
980 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
981 s_filter_params.pes_type = DMX_PES_AUDIO1;
984 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
985 s_filter_params.pes_type = DMX_PES_TELETEXT1;
988 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
989 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
992 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
993 s_filter_params.pes_type = DMX_PES_PCR1;
997 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
998 s_filter_params.pes_type = DMX_PES_VIDEO2;
1001 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1002 s_filter_params.pes_type = DMX_PES_AUDIO2;
1005 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1006 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1009 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1010 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1013 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1014 s_filter_params.pes_type = DMX_PES_PCR2;
1018 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1019 s_filter_params.pes_type = DMX_PES_VIDEO3;
1022 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1023 s_filter_params.pes_type = DMX_PES_AUDIO3;
1026 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1027 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1030 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1031 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1034 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1035 s_filter_params.pes_type = DMX_PES_PCR3;
1037 /* Usually used by Nova cards */
1040 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1041 s_filter_params.pes_type = DMX_PES_OTHER;
1045 /* We then give the order to the device : */
1046 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1048 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1050 return VLC_EGENERIC;
1055 /*****************************************************************************
1056 * DMXUnsetFilter : removes a filter
1057 *****************************************************************************/
1058 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1062 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1064 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1065 i_ret, strerror(errno) );
1069 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1079 /*****************************************************************************
1081 *****************************************************************************/
1082 int E_(DVROpen)( access_t * p_access )
1084 access_sys_t *p_sys = p_access->p_sys;
1085 unsigned int i_adapter, i_device;
1089 var_Get( p_access, "dvb-adapter", &val );
1090 i_adapter = val.i_int;
1091 var_Get( p_access, "dvb-device", &val );
1092 i_device = val.i_int;
1094 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1095 >= (int)sizeof(dvr) )
1097 msg_Err( p_access, "snprintf() truncated string for DVR" );
1098 dvr[sizeof(dvr) - 1] = '\0';
1101 msg_Dbg( p_access, "Opening device %s", dvr );
1102 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1104 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1106 return VLC_EGENERIC;
1112 /*****************************************************************************
1114 *****************************************************************************/
1115 void E_(DVRClose)( access_t * p_access )
1117 access_sys_t *p_sys = p_access->p_sys;
1119 close( p_sys->i_handle );
1127 /*****************************************************************************
1129 *****************************************************************************/
1130 int E_(CAMOpen)( access_t *p_access )
1132 access_sys_t *p_sys = p_access->p_sys;
1134 int i_adapter, i_device, i_slot;
1137 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1138 i_device = var_GetInteger( p_access, "dvb-device" );
1140 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1142 msg_Err( p_access, "snprintf() truncated string for CA" );
1143 ca[sizeof(ca) - 1] = '\0';
1146 msg_Dbg( p_access, "Opening device %s", ca );
1147 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1149 msg_Err( p_access, "CAMInit: opening device failed (%s)",
1151 p_sys->i_ca_handle = 0;
1152 return VLC_EGENERIC;
1155 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1156 || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1158 msg_Err( p_access, "CAMInit: no compatible CAM module" );
1159 close( p_sys->i_ca_handle );
1160 p_sys->i_ca_handle = 0;
1161 return VLC_EGENERIC;
1164 p_sys->i_nb_slots = caps.slot_num;
1165 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1167 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1169 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1171 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1175 msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1176 p_sys->i_nb_slots );
1178 p_sys->i_ca_timeout = 100000;
1179 /* Wait a bit otherwise it doesn't initialize properly... */
1185 /*****************************************************************************
1187 *****************************************************************************/
1188 int E_(CAMPoll)( access_t * p_access )
1190 access_sys_t *p_sys = p_access->p_sys;
1192 if ( p_sys->i_ca_handle == 0 )
1194 return VLC_EGENERIC;
1197 return E_(en50221_Poll)( p_access );
1200 /*****************************************************************************
1202 *****************************************************************************/
1203 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1205 access_sys_t *p_sys = p_access->p_sys;
1207 if ( p_sys->i_ca_handle == 0 )
1209 return VLC_EGENERIC;
1212 E_(en50221_SetCAPMT)( p_access, p_pmt );
1217 /*****************************************************************************
1219 *****************************************************************************/
1220 void E_(CAMClose)( access_t * p_access )
1222 access_sys_t *p_sys = p_access->p_sys;
1224 E_(en50221_End)( p_access );
1226 if ( p_sys->i_ca_handle )
1228 close( p_sys->i_ca_handle );