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 #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;
695 /* Prepare the fep structure */
696 var_Get( p_access, "dvb-frequency", &val );
697 i_frequency = val.i_int;
698 var_Get( p_access, "dvb-lnb-slof", &val );
699 i_lnb_slof = val.i_int;
701 if( i_frequency >= i_lnb_slof )
702 var_Get( p_access, "dvb-lnb-lof2", &val );
704 var_Get( p_access, "dvb-lnb-lof1", &val );
705 fep.frequency = i_frequency - val.i_int;
707 fep.inversion = DecodeInversion( p_access );
709 var_Get( p_access, "dvb-srate", &val );
710 fep.u.qpsk.symbol_rate = val.i_int;
712 var_Get( p_access, "dvb-fec", &val );
713 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
715 if( DoDiseqc( p_access ) < 0 )
720 /* Empty the event queue */
723 struct dvb_frontend_event event;
724 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
725 && errno == EWOULDBLOCK )
729 /* Now send it all to the frontend device */
730 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
732 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
740 /*****************************************************************************
741 * FrontendSetQAM : controls the FE device
742 *****************************************************************************/
743 static int FrontendSetQAM( access_t *p_access )
745 access_sys_t *p_sys = p_access->p_sys;
746 struct dvb_frontend_parameters fep;
750 /* Prepare the fep structure */
752 var_Get( p_access, "dvb-frequency", &val );
753 fep.frequency = val.i_int;
755 fep.inversion = DecodeInversion( p_access );
757 var_Get( p_access, "dvb-srate", &val );
758 fep.u.qam.symbol_rate = val.i_int;
760 var_Get( p_access, "dvb-fec", &val );
761 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
763 fep.u.qam.modulation = DecodeModulation( p_access );
765 /* Empty the event queue */
768 struct dvb_frontend_event event;
769 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
770 && errno == EWOULDBLOCK )
774 /* Now send it all to the frontend device */
775 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
777 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
785 /*****************************************************************************
786 * FrontendSetOFDM : controls the FE device
787 *****************************************************************************/
788 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
791 fe_bandwidth_t fe_bandwidth = 0;
793 var_Get( p_access, "dvb-bandwidth", &val );
794 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
798 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
799 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
800 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
801 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
803 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
804 fe_bandwidth = BANDWIDTH_AUTO;
810 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
813 fe_transmit_mode_t fe_transmission = 0;
815 var_Get( p_access, "dvb-transmission", &val );
816 msg_Dbg( p_access, "using transmission=%d", val.i_int );
820 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
821 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
822 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
824 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
825 fe_transmission = TRANSMISSION_MODE_AUTO;
828 return fe_transmission;
831 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
834 fe_guard_interval_t fe_guard = 0;
836 var_Get( p_access, "dvb-guard", &val );
837 msg_Dbg( p_access, "using guard=%d", val.i_int );
841 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
842 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
843 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
844 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
845 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
847 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
848 fe_guard = GUARD_INTERVAL_AUTO;
854 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
857 fe_hierarchy_t fe_hierarchy = 0;
859 var_Get( p_access, "dvb-hierarchy", &val );
860 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
864 case -1: fe_hierarchy = HIERARCHY_NONE; break;
865 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
866 case 1: fe_hierarchy = HIERARCHY_1; break;
867 case 2: fe_hierarchy = HIERARCHY_2; break;
868 case 4: fe_hierarchy = HIERARCHY_4; break;
870 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
871 fe_hierarchy = HIERARCHY_AUTO;
877 static int FrontendSetOFDM( access_t * p_access )
879 access_sys_t *p_sys = p_access->p_sys;
880 struct dvb_frontend_parameters fep;
884 /* Prepare the fep structure */
886 var_Get( p_access, "dvb-frequency", &val );
887 fep.frequency = val.i_int;
889 fep.inversion = DecodeInversion( p_access );
891 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
892 var_Get( p_access, "dvb-code-rate-hp", &val );
893 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
894 var_Get( p_access, "dvb-code-rate-lp", &val );
895 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
896 fep.u.ofdm.constellation = DecodeModulation( p_access );
897 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
898 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
899 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
901 /* Empty the event queue */
904 struct dvb_frontend_event event;
905 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
906 && errno == EWOULDBLOCK )
910 /* Now send it all to the frontend device */
911 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
913 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
926 /*****************************************************************************
927 * DMXSetFilter : controls the demux to add a filter
928 *****************************************************************************/
929 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
931 struct dmx_pes_filter_params s_filter_params;
933 unsigned int i_adapter, i_device;
937 var_Get( p_access, "dvb-adapter", &val );
938 i_adapter = val.i_int;
939 var_Get( p_access, "dvb-device", &val );
940 i_device = val.i_int;
942 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
943 >= (int)sizeof(dmx) )
945 msg_Err( p_access, "snprintf() truncated string for DMX" );
946 dmx[sizeof(dmx) - 1] = '\0';
949 msg_Dbg( p_access, "Opening device %s", dmx );
950 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
952 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
957 /* We fill the DEMUX structure : */
958 s_filter_params.pid = i_pid;
959 s_filter_params.input = DMX_IN_FRONTEND;
960 s_filter_params.output = DMX_OUT_TS_TAP;
961 s_filter_params.flags = DMX_IMMEDIATE_START;
966 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
967 s_filter_params.pes_type = DMX_PES_VIDEO0;
970 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
971 s_filter_params.pes_type = DMX_PES_AUDIO0;
974 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
975 s_filter_params.pes_type = DMX_PES_TELETEXT0;
978 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
979 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
982 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
983 s_filter_params.pes_type = DMX_PES_PCR0;
987 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
988 s_filter_params.pes_type = DMX_PES_VIDEO1;
991 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
992 s_filter_params.pes_type = DMX_PES_AUDIO1;
995 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
996 s_filter_params.pes_type = DMX_PES_TELETEXT1;
999 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1000 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1003 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1004 s_filter_params.pes_type = DMX_PES_PCR1;
1008 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1009 s_filter_params.pes_type = DMX_PES_VIDEO2;
1012 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1013 s_filter_params.pes_type = DMX_PES_AUDIO2;
1016 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1017 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1020 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1021 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1024 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1025 s_filter_params.pes_type = DMX_PES_PCR2;
1029 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1030 s_filter_params.pes_type = DMX_PES_VIDEO3;
1033 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1034 s_filter_params.pes_type = DMX_PES_AUDIO3;
1037 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1038 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1041 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1042 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1045 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1046 s_filter_params.pes_type = DMX_PES_PCR3;
1048 /* Usually used by Nova cards */
1051 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1052 s_filter_params.pes_type = DMX_PES_OTHER;
1056 /* We then give the order to the device : */
1057 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1059 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1061 return VLC_EGENERIC;
1066 /*****************************************************************************
1067 * DMXUnsetFilter : removes a filter
1068 *****************************************************************************/
1069 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1073 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1075 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1076 i_ret, strerror(errno) );
1080 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1090 /*****************************************************************************
1092 *****************************************************************************/
1093 int E_(DVROpen)( access_t * p_access )
1095 access_sys_t *p_sys = p_access->p_sys;
1096 unsigned int i_adapter, i_device;
1100 var_Get( p_access, "dvb-adapter", &val );
1101 i_adapter = val.i_int;
1102 var_Get( p_access, "dvb-device", &val );
1103 i_device = val.i_int;
1105 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1106 >= (int)sizeof(dvr) )
1108 msg_Err( p_access, "snprintf() truncated string for DVR" );
1109 dvr[sizeof(dvr) - 1] = '\0';
1112 msg_Dbg( p_access, "Opening device %s", dvr );
1113 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1115 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1117 return VLC_EGENERIC;
1123 /*****************************************************************************
1125 *****************************************************************************/
1126 void E_(DVRClose)( access_t * p_access )
1128 access_sys_t *p_sys = p_access->p_sys;
1130 close( p_sys->i_handle );
1138 /*****************************************************************************
1140 *****************************************************************************/
1141 int E_(CAMOpen)( access_t *p_access )
1143 access_sys_t *p_sys = p_access->p_sys;
1145 int i_adapter, i_device, i_slot;
1148 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1149 i_device = var_GetInteger( p_access, "dvb-device" );
1151 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1153 msg_Err( p_access, "snprintf() truncated string for CA" );
1154 ca[sizeof(ca) - 1] = '\0';
1157 msg_Dbg( p_access, "Opening device %s", ca );
1158 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1160 msg_Err( p_access, "CAMInit: opening device failed (%s)",
1162 p_sys->i_ca_handle = 0;
1163 return VLC_EGENERIC;
1166 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1167 || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1169 msg_Err( p_access, "CAMInit: no compatible CAM module" );
1170 close( p_sys->i_ca_handle );
1171 p_sys->i_ca_handle = 0;
1172 return VLC_EGENERIC;
1175 p_sys->i_nb_slots = caps.slot_num;
1176 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1178 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1180 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1182 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1186 msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1187 p_sys->i_nb_slots );
1189 p_sys->i_ca_timeout = 100000;
1190 /* Wait a bit otherwise it doesn't initialize properly... */
1196 /*****************************************************************************
1198 *****************************************************************************/
1199 int E_(CAMPoll)( access_t * p_access )
1201 access_sys_t *p_sys = p_access->p_sys;
1203 if ( p_sys->i_ca_handle == 0 )
1205 return VLC_EGENERIC;
1208 return E_(en50221_Poll)( p_access );
1211 /*****************************************************************************
1213 *****************************************************************************/
1214 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1216 access_sys_t *p_sys = p_access->p_sys;
1218 if ( p_sys->i_ca_handle == 0 )
1220 return VLC_EGENERIC;
1223 E_(en50221_SetCAPMT)( p_access, p_pmt );
1228 /*****************************************************************************
1230 *****************************************************************************/
1231 void E_(CAMClose)( access_t * p_access )
1233 access_sys_t *p_sys = p_access->p_sys;
1235 E_(en50221_End)( p_access );
1237 if ( p_sys->i_ca_handle )
1239 close( p_sys->i_ca_handle );