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;
262 struct dvb_frontend_event event;
263 fe_status_t i_status, i_diff;
265 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT,
268 msg_Err( p_access, "reading frontend status failed (%d) %s",
269 i_ret, strerror(errno) );
273 i_status = event.status;
274 i_diff = i_status ^ p_frontend->i_last_status;
275 p_frontend->i_last_status = i_status;
279 if ( i_diff & (x) ) \
281 if ( i_status & (x) )
284 IF_UP( FE_HAS_SIGNAL )
285 msg_Dbg( p_access, "frontend has acquired signal" );
287 msg_Dbg( p_access, "frontend has lost signal" );
289 IF_UP( FE_HAS_CARRIER )
290 msg_Dbg( p_access, "frontend has acquired carrier" );
292 msg_Dbg( p_access, "frontend has lost carrier" );
294 IF_UP( FE_HAS_VITERBI )
295 msg_Dbg( p_access, "frontend has acquired stable FEC" );
297 msg_Dbg( p_access, "frontend has lost FEC" );
300 msg_Dbg( p_access, "frontend has acquired sync" );
302 msg_Dbg( p_access, "frontend has lost sync" );
307 msg_Dbg( p_access, "frontend has acquired lock" );
308 p_sys->i_frontend_timeout = 0;
310 /* Read some statistics */
311 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
312 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
313 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
314 msg_Dbg( p_access, "- Signal strength: %d", i_value );
315 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
316 msg_Dbg( p_access, "- SNR: %d", i_value );
320 msg_Dbg( p_access, "frontend has lost lock" );
321 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
326 /* The frontend was reinited. */
327 msg_Warn( p_access, "reiniting frontend");
328 E_(FrontendSet)( p_access );
332 /*****************************************************************************
333 * FrontendInfo : Return information about given frontend
334 *****************************************************************************/
335 static int FrontendInfo( access_t *p_access )
337 access_sys_t *p_sys = p_access->p_sys;
338 frontend_t *p_frontend = p_sys->p_frontend;
341 /* Determine type of frontend */
342 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
343 &p_frontend->info )) < 0 )
345 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
350 /* Print out frontend capabilities. */
351 msg_Dbg(p_access, "Frontend Info:" );
352 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
353 switch( p_frontend->info.type )
356 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
359 msg_Dbg( p_access, " type = QAM (DVB-C)" );
362 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
364 #if 0 /* DVB_API_VERSION == 3 */
366 msg_Dbg(p_access, " type = MEMORY" );
369 msg_Dbg(p_access, " type = NETWORK" );
373 msg_Err( p_access, " unknown frontend type (%d)",
374 p_frontend->info.type );
377 msg_Dbg(p_access, " frequency_min = %u (kHz)",
378 p_frontend->info.frequency_min);
379 msg_Dbg(p_access, " frequency_max = %u (kHz)",
380 p_frontend->info.frequency_max);
381 msg_Dbg(p_access, " frequency_stepsize = %u",
382 p_frontend->info.frequency_stepsize);
383 msg_Dbg(p_access, " frequency_tolerance = %u",
384 p_frontend->info.frequency_tolerance);
385 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
386 p_frontend->info.symbol_rate_min);
387 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
388 p_frontend->info.symbol_rate_max);
389 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
390 p_frontend->info.symbol_rate_tolerance);
391 msg_Dbg(p_access, " notifier_delay (ms) = %u",
392 p_frontend->info.notifier_delay );
394 msg_Dbg(p_access, "Frontend Info capability list:");
395 if( p_frontend->info.caps & FE_IS_STUPID)
396 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
397 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
398 msg_Dbg(p_access, " inversion auto");
399 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
400 msg_Dbg(p_access, " forward error correction 1/2");
401 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
402 msg_Dbg(p_access, " forward error correction 2/3");
403 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
404 msg_Dbg(p_access, " forward error correction 3/4");
405 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
406 msg_Dbg(p_access, " forward error correction 4/5");
407 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
408 msg_Dbg(p_access, " forward error correction 5/6");
409 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
410 msg_Dbg(p_access, " forward error correction 6/7");
411 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
412 msg_Dbg(p_access, " forward error correction 7/8");
413 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
414 msg_Dbg(p_access, " forward error correction 8/9");
415 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
416 msg_Dbg(p_access, " forward error correction auto");
417 if( p_frontend->info.caps & FE_CAN_QPSK)
418 msg_Dbg(p_access, " card can do QPSK");
419 if( p_frontend->info.caps & FE_CAN_QAM_16)
420 msg_Dbg(p_access, " card can do QAM 16");
421 if( p_frontend->info.caps & FE_CAN_QAM_32)
422 msg_Dbg(p_access, " card can do QAM 32");
423 if( p_frontend->info.caps & FE_CAN_QAM_64)
424 msg_Dbg(p_access, " card can do QAM 64");
425 if( p_frontend->info.caps & FE_CAN_QAM_128)
426 msg_Dbg(p_access, " card can do QAM 128");
427 if( p_frontend->info.caps & FE_CAN_QAM_256)
428 msg_Dbg(p_access, " card can do QAM 256");
429 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
430 msg_Dbg(p_access, " card can do QAM auto");
431 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
432 msg_Dbg(p_access, " transmission mode auto");
433 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
434 msg_Dbg(p_access, " bandwidth mode auto");
435 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
436 msg_Dbg(p_access, " guard interval mode auto");
437 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
438 msg_Dbg(p_access, " hierarchy mode auto");
439 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
440 msg_Dbg(p_access, " card can mute TS");
441 if( p_frontend->info.caps & FE_CAN_RECOVER)
442 msg_Dbg(p_access, " card can recover from a cable unplug");
443 msg_Dbg(p_access, "End of capability list");
448 /*****************************************************************************
449 * Decoding the DVB parameters (common)
450 *****************************************************************************/
451 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
454 fe_spectral_inversion_t fe_inversion = 0;
456 var_Get( p_access, "dvb-inversion", &val );
457 msg_Dbg( p_access, "using inversion=%d", val.i_int );
461 case 0: fe_inversion = INVERSION_OFF; break;
462 case 1: fe_inversion = INVERSION_ON; break;
463 case 2: fe_inversion = INVERSION_AUTO; break;
465 msg_Dbg( p_access, "dvb has inversion not set, using auto");
466 fe_inversion = INVERSION_AUTO;
472 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
474 fe_code_rate_t fe_fec = FEC_NONE;
476 msg_Dbg( p_access, "using fec=%d", i_val );
480 case 0: fe_fec = FEC_NONE; break;
481 case 1: fe_fec = FEC_1_2; break;
482 case 2: fe_fec = FEC_2_3; break;
483 case 3: fe_fec = FEC_3_4; break;
484 case 4: fe_fec = FEC_4_5; break;
485 case 5: fe_fec = FEC_5_6; break;
486 case 6: fe_fec = FEC_6_7; break;
487 case 7: fe_fec = FEC_7_8; break;
488 case 8: fe_fec = FEC_8_9; break;
489 case 9: fe_fec = FEC_AUTO; break;
493 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
499 static fe_modulation_t DecodeModulation( access_t *p_access )
502 fe_modulation_t fe_modulation = 0;
504 var_Get( p_access, "dvb-modulation", &val );
508 case -1: fe_modulation = QPSK; break;
509 case 0: fe_modulation = QAM_AUTO; break;
510 case 16: fe_modulation = QAM_16; break;
511 case 32: fe_modulation = QAM_32; break;
512 case 64: fe_modulation = QAM_64; break;
513 case 128: fe_modulation = QAM_128; break;
514 case 256: fe_modulation = QAM_256; break;
516 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
517 fe_modulation = QAM_AUTO;
520 return fe_modulation;
523 /*****************************************************************************
524 * FrontendSetQPSK : controls the FE device
525 *****************************************************************************/
526 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
529 fe_sec_voltage_t fe_voltage;
531 var_Get( p_access, "dvb-voltage", &val );
532 msg_Dbg( p_access, "using voltage=%d", val.i_int );
536 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
537 case 13: fe_voltage = SEC_VOLTAGE_13; break;
538 case 18: fe_voltage = SEC_VOLTAGE_18; break;
540 fe_voltage = SEC_VOLTAGE_OFF;
541 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
547 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
550 fe_sec_tone_mode_t fe_tone;
552 var_Get( p_access, "dvb-tone", &val );
553 msg_Dbg( p_access, "using tone=%d", val.i_int );
557 case 0: fe_tone = SEC_TONE_OFF; break;
558 case 1: fe_tone = SEC_TONE_ON; break;
560 fe_tone = SEC_TONE_OFF;
561 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
569 struct dvb_diseqc_master_cmd cmd;
573 static int DoDiseqc( access_t *p_access )
575 access_sys_t *p_sys = p_access->p_sys;
577 int i_frequency, i_lnb_slof;
578 fe_sec_voltage_t fe_voltage;
579 fe_sec_tone_mode_t fe_tone;
582 var_Get( p_access, "dvb-frequency", &val );
583 i_frequency = val.i_int;
584 var_Get( p_access, "dvb-lnb-slof", &val );
585 i_lnb_slof = val.i_int;
587 var_Get( p_access, "dvb-tone", &val );
588 if( val.i_int == -1 /* auto */ )
590 if( i_frequency >= i_lnb_slof )
594 var_Set( p_access, "dvb-tone", val );
597 fe_voltage = DecodeVoltage( p_access );
598 fe_tone = DecodeTone( p_access );
600 /* Switch off continuous tone. */
601 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
603 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
604 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
609 /* Configure LNB voltage. */
610 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
612 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
613 fe_voltage, i_err, strerror(errno) );
617 var_Get( p_access, "dvb-high-voltage", &val );
618 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
619 val.b_bool )) < 0 && val.b_bool )
622 "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
623 val.b_bool, i_err, strerror(errno) );
626 /* Wait for at least 15 ms. */
629 var_Get( p_access, "dvb-satno", &val );
630 if( val.i_int > 0 && val.i_int < 5 )
632 /* digital satellite equipment control,
633 * specification is available from http://www.eutelsat.com/
636 /* 1.x compatible equipment */
637 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
639 /* param: high nibble: reset bits, low nibble set bits,
640 * bits are: option, position, polarization, band
642 cmd.cmd.msg[3] = 0xf0 /* reset bits */
643 | (((val.i_int - 1) * 4) & 0xc)
644 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
645 | (fe_tone == SEC_TONE_ON ? 1 : 0);
647 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
650 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
651 i_err, strerror(errno) );
655 msleep(15000 + cmd.wait * 1000);
657 /* A or B simple diseqc ("diseqc-compatible") */
658 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
659 ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
661 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
662 i_err, strerror(errno) );
669 if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
671 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
672 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
681 static int FrontendSetQPSK( access_t *p_access )
683 access_sys_t *p_sys = p_access->p_sys;
684 struct dvb_frontend_parameters fep;
687 int i_frequency, i_lnb_slof;
689 /* Prepare the fep structure */
690 var_Get( p_access, "dvb-frequency", &val );
691 i_frequency = val.i_int;
692 var_Get( p_access, "dvb-lnb-slof", &val );
693 i_lnb_slof = val.i_int;
695 if( i_frequency >= i_lnb_slof )
696 var_Get( p_access, "dvb-lnb-lof2", &val );
698 var_Get( p_access, "dvb-lnb-lof1", &val );
699 fep.frequency = i_frequency - val.i_int;
701 fep.inversion = DecodeInversion( p_access );
703 var_Get( p_access, "dvb-srate", &val );
704 fep.u.qpsk.symbol_rate = val.i_int;
706 var_Get( p_access, "dvb-fec", &val );
707 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
709 if( DoDiseqc( p_access ) < 0 )
714 /* Empty the event queue */
717 struct dvb_frontend_event event;
718 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
719 && errno == EWOULDBLOCK )
723 /* Now send it all to the frontend device */
724 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
726 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
734 /*****************************************************************************
735 * FrontendSetQAM : controls the FE device
736 *****************************************************************************/
737 static int FrontendSetQAM( access_t *p_access )
739 access_sys_t *p_sys = p_access->p_sys;
740 struct dvb_frontend_parameters fep;
744 /* Prepare the fep structure */
746 var_Get( p_access, "dvb-frequency", &val );
747 fep.frequency = val.i_int;
749 fep.inversion = DecodeInversion( p_access );
751 var_Get( p_access, "dvb-srate", &val );
752 fep.u.qam.symbol_rate = val.i_int;
754 var_Get( p_access, "dvb-fec", &val );
755 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
757 fep.u.qam.modulation = DecodeModulation( p_access );
759 /* Empty the event queue */
762 struct dvb_frontend_event event;
763 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
764 && errno == EWOULDBLOCK )
768 /* Now send it all to the frontend device */
769 if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
771 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
779 /*****************************************************************************
780 * FrontendSetOFDM : controls the FE device
781 *****************************************************************************/
782 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
785 fe_bandwidth_t fe_bandwidth = 0;
787 var_Get( p_access, "dvb-bandwidth", &val );
788 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
792 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
793 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
794 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
795 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
797 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
798 fe_bandwidth = BANDWIDTH_AUTO;
804 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
807 fe_transmit_mode_t fe_transmission = 0;
809 var_Get( p_access, "dvb-transmission", &val );
810 msg_Dbg( p_access, "using transmission=%d", val.i_int );
814 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
815 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
816 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
818 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
819 fe_transmission = TRANSMISSION_MODE_AUTO;
822 return fe_transmission;
825 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
828 fe_guard_interval_t fe_guard = 0;
830 var_Get( p_access, "dvb-guard", &val );
831 msg_Dbg( p_access, "using guard=%d", val.i_int );
835 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
836 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
837 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
838 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
839 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
841 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
842 fe_guard = GUARD_INTERVAL_AUTO;
848 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
851 fe_hierarchy_t fe_hierarchy = 0;
853 var_Get( p_access, "dvb-hierarchy", &val );
854 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
858 case -1: fe_hierarchy = HIERARCHY_NONE; break;
859 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
860 case 1: fe_hierarchy = HIERARCHY_1; break;
861 case 2: fe_hierarchy = HIERARCHY_2; break;
862 case 4: fe_hierarchy = HIERARCHY_4; break;
864 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
865 fe_hierarchy = HIERARCHY_AUTO;
871 static int FrontendSetOFDM( access_t * p_access )
873 access_sys_t *p_sys = p_access->p_sys;
874 struct dvb_frontend_parameters fep;
878 /* Prepare the fep structure */
880 var_Get( p_access, "dvb-frequency", &val );
881 fep.frequency = val.i_int;
883 fep.inversion = DecodeInversion( p_access );
885 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
886 var_Get( p_access, "dvb-code-rate-hp", &val );
887 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
888 var_Get( p_access, "dvb-code-rate-lp", &val );
889 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
890 fep.u.ofdm.constellation = DecodeModulation( p_access );
891 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
892 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
893 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
895 /* Empty the event queue */
898 struct dvb_frontend_event event;
899 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
900 && errno == EWOULDBLOCK )
904 /* Now send it all to the frontend device */
905 if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
907 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
920 /*****************************************************************************
921 * DMXSetFilter : controls the demux to add a filter
922 *****************************************************************************/
923 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
925 struct dmx_pes_filter_params s_filter_params;
927 unsigned int i_adapter, i_device;
931 var_Get( p_access, "dvb-adapter", &val );
932 i_adapter = val.i_int;
933 var_Get( p_access, "dvb-device", &val );
934 i_device = val.i_int;
936 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
937 >= (int)sizeof(dmx) )
939 msg_Err( p_access, "snprintf() truncated string for DMX" );
940 dmx[sizeof(dmx) - 1] = '\0';
943 msg_Dbg( p_access, "Opening device %s", dmx );
944 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
946 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
951 /* We fill the DEMUX structure : */
952 s_filter_params.pid = i_pid;
953 s_filter_params.input = DMX_IN_FRONTEND;
954 s_filter_params.output = DMX_OUT_TS_TAP;
955 s_filter_params.flags = DMX_IMMEDIATE_START;
960 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
961 s_filter_params.pes_type = DMX_PES_VIDEO0;
964 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
965 s_filter_params.pes_type = DMX_PES_AUDIO0;
968 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
969 s_filter_params.pes_type = DMX_PES_TELETEXT0;
972 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
973 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
976 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
977 s_filter_params.pes_type = DMX_PES_PCR0;
981 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
982 s_filter_params.pes_type = DMX_PES_VIDEO1;
985 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
986 s_filter_params.pes_type = DMX_PES_AUDIO1;
989 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
990 s_filter_params.pes_type = DMX_PES_TELETEXT1;
993 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
994 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
997 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
998 s_filter_params.pes_type = DMX_PES_PCR1;
1002 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1003 s_filter_params.pes_type = DMX_PES_VIDEO2;
1006 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1007 s_filter_params.pes_type = DMX_PES_AUDIO2;
1010 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1011 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1014 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1015 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1018 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1019 s_filter_params.pes_type = DMX_PES_PCR2;
1023 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1024 s_filter_params.pes_type = DMX_PES_VIDEO3;
1027 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1028 s_filter_params.pes_type = DMX_PES_AUDIO3;
1031 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1032 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1035 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1036 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1039 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1040 s_filter_params.pes_type = DMX_PES_PCR3;
1042 /* Usually used by Nova cards */
1045 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1046 s_filter_params.pes_type = DMX_PES_OTHER;
1050 /* We then give the order to the device : */
1051 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1053 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1055 return VLC_EGENERIC;
1060 /*****************************************************************************
1061 * DMXUnsetFilter : removes a filter
1062 *****************************************************************************/
1063 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1067 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1069 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1070 i_ret, strerror(errno) );
1074 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1084 /*****************************************************************************
1086 *****************************************************************************/
1087 int E_(DVROpen)( access_t * p_access )
1089 access_sys_t *p_sys = p_access->p_sys;
1090 unsigned int i_adapter, i_device;
1094 var_Get( p_access, "dvb-adapter", &val );
1095 i_adapter = val.i_int;
1096 var_Get( p_access, "dvb-device", &val );
1097 i_device = val.i_int;
1099 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1100 >= (int)sizeof(dvr) )
1102 msg_Err( p_access, "snprintf() truncated string for DVR" );
1103 dvr[sizeof(dvr) - 1] = '\0';
1106 msg_Dbg( p_access, "Opening device %s", dvr );
1107 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1109 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1111 return VLC_EGENERIC;
1117 /*****************************************************************************
1119 *****************************************************************************/
1120 void E_(DVRClose)( access_t * p_access )
1122 access_sys_t *p_sys = p_access->p_sys;
1124 close( p_sys->i_handle );
1132 /*****************************************************************************
1134 *****************************************************************************/
1135 int E_(CAMOpen)( access_t *p_access )
1137 access_sys_t *p_sys = p_access->p_sys;
1139 int i_adapter, i_device, i_slot;
1142 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1143 i_device = var_GetInteger( p_access, "dvb-device" );
1145 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1147 msg_Err( p_access, "snprintf() truncated string for CA" );
1148 ca[sizeof(ca) - 1] = '\0';
1151 msg_Dbg( p_access, "Opening device %s", ca );
1152 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1154 msg_Err( p_access, "CAMInit: opening device failed (%s)",
1156 p_sys->i_ca_handle = 0;
1157 return VLC_EGENERIC;
1160 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1161 || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1163 msg_Err( p_access, "CAMInit: no compatible CAM module" );
1164 close( p_sys->i_ca_handle );
1165 p_sys->i_ca_handle = 0;
1166 return VLC_EGENERIC;
1169 p_sys->i_nb_slots = caps.slot_num;
1170 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1172 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1174 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1176 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1180 msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1181 p_sys->i_nb_slots );
1183 p_sys->i_ca_timeout = 100000;
1184 /* Wait a bit otherwise it doesn't initialize properly... */
1190 /*****************************************************************************
1192 *****************************************************************************/
1193 int E_(CAMPoll)( access_t * p_access )
1195 access_sys_t *p_sys = p_access->p_sys;
1197 if ( p_sys->i_ca_handle == 0 )
1199 return VLC_EGENERIC;
1202 return E_(en50221_Poll)( p_access );
1205 /*****************************************************************************
1207 *****************************************************************************/
1208 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1210 access_sys_t *p_sys = p_access->p_sys;
1212 if ( p_sys->i_ca_handle == 0 )
1214 return VLC_EGENERIC;
1217 E_(en50221_SetCAPMT)( p_access, p_pmt );
1222 /*****************************************************************************
1224 *****************************************************************************/
1225 void E_(CAMClose)( access_t * p_access )
1227 access_sys_t *p_sys = p_access->p_sys;
1229 E_(en50221_End)( p_access );
1231 if ( p_sys->i_ca_handle )
1233 close( p_sys->i_ca_handle );