1 /*****************************************************************************
2 * 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>
37 #ifdef HAVE_INTTYPES_H
38 # include <inttypes.h> /* int16_t .. */
40 #include <sys/types.h>
45 #include <sys/types.h>
49 /* DVB Card Drivers */
50 #include <linux/dvb/version.h>
51 #include <linux/dvb/dmx.h>
52 #include <linux/dvb/frontend.h>
54 #include <linux/errno.h>
63 typedef struct frontend_t {
65 struct dvb_frontend_info info;
68 /* Local prototypes */
69 static int FrontendInfo( input_thread_t * p_input );
70 static int FrontendSetQPSK( input_thread_t * p_input );
71 static int FrontendSetQAM( input_thread_t * p_input );
72 static int FrontendSetOFDM( input_thread_t * p_input );
73 static int FrontendCheck( input_thread_t * p_input );
75 /*****************************************************************************
76 * FrontendOpen : Determine frontend device information and capabilities
77 *****************************************************************************/
78 int E_(FrontendOpen)( input_thread_t * p_input )
80 thread_dvb_data_t * p_dvb
81 = (thread_dvb_data_t *)p_input->p_access_data;
82 frontend_t * p_frontend;
83 unsigned int i_adapter, i_device;
88 var_Get( p_input, "dvb-adapter", &val );
89 i_adapter = val.i_int;
90 var_Get( p_input, "dvb-device", &val );
92 var_Get( p_input, "dvb-probe", &val );
95 if ( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device )
96 >= (int)sizeof(frontend) )
98 msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
99 frontend[sizeof(frontend) - 1] = '\0';
102 p_frontend = malloc(sizeof(frontend_t));
103 p_dvb->p_frontend = p_frontend;
105 msg_Dbg( p_input, "Opening device %s", frontend );
106 if ( (p_frontend->i_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
108 msg_Err( p_input, "FrontEndOpen: opening device failed (%s)",
116 char * psz_expected = NULL;
119 if ( FrontendInfo( p_input ) < 0 )
121 close( p_frontend->i_handle );
126 switch ( p_frontend->info.type )
138 psz_real = "unknown";
142 if( ((strncmp( p_input->psz_access, "qpsk", 4 ) == 0) ||
143 (strncmp( p_input->psz_access, "dvb-s", 5 ) == 0) ||
144 (strncmp( p_input->psz_access, "satellite", 9 ) == 0) ) &&
145 (p_frontend->info.type != FE_QPSK) )
147 psz_expected = "DVB-S";
149 if( ((strncmp( p_input->psz_access, "cable", 5 ) == 0) ||
150 (strncmp( p_input->psz_access, "dvb-c", 5 ) == 0) ) &&
151 (p_frontend->info.type != FE_QAM) )
153 psz_expected = "DVB-C";
155 if( ((strncmp( p_input->psz_access, "terrestrial", 11 ) == 0) ||
156 (strncmp( p_input->psz_access, "dvb-t", 5 ) == 0) ) &&
157 (p_frontend->info.type != FE_OFDM) )
159 psz_expected = "DVB-T";
162 if ( psz_expected != NULL )
164 msg_Err( p_input, "the user asked for %s, and the tuner is %s",
165 psz_expected, psz_real );
166 close( p_frontend->i_handle );
171 else /* no frontend probing is done so use default border values. */
173 msg_Dbg( p_input, "using default values for frontend info" );
175 msg_Dbg( p_input, "method of access is %s", p_input->psz_access );
176 p_frontend->info.type = FE_QPSK;
177 if ( !strncmp( p_input->psz_access, "qpsk", 4 ) ||
178 !strncmp( p_input->psz_access, "dvb-s", 5 ) )
179 p_frontend->info.type = FE_QPSK;
180 else if ( !strncmp( p_input->psz_access, "cable", 5 ) ||
181 !strncmp( p_input->psz_access, "dvb-c", 5 ) )
182 p_frontend->info.type = FE_QAM;
183 else if ( !strncmp( p_input->psz_access, "terrestrial", 11 ) ||
184 !strncmp( p_input->psz_access, "dvb-t", 5 ) )
185 p_frontend->info.type = FE_OFDM;
191 /*****************************************************************************
192 * FrontendClose : Close the frontend
193 *****************************************************************************/
194 void E_(FrontendClose)( input_thread_t * p_input )
196 thread_dvb_data_t * p_dvb
197 = (thread_dvb_data_t *)p_input->p_access_data;
198 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
200 if ( p_frontend != NULL )
202 close( p_frontend->i_handle );
207 /*****************************************************************************
208 * FrontendSet : Tune !
209 *****************************************************************************/
210 int E_(FrontendSet)( input_thread_t * p_input )
212 thread_dvb_data_t * p_dvb
213 = (thread_dvb_data_t *)p_input->p_access_data;
214 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
216 switch ( p_frontend->info.type )
218 /* DVB-S: satellite and budget cards (nova) */
220 if ( FrontendSetQPSK( p_input ) < 0 )
222 msg_Err( p_input, "DVB-S: tuning failed" );
229 if ( FrontendSetQAM( p_input ) < 0 )
231 msg_Err( p_input, "DVB-C: tuning failed" );
238 if ( FrontendSetOFDM( p_input ) < 0 )
240 msg_Err( p_input, "DVB-T: tuning failed" );
246 msg_Err( p_input, "Could not determine frontend type on %s",
247 p_frontend->info.name );
253 /*****************************************************************************
254 * FrontendInfo : Return information about given frontend
255 *****************************************************************************/
256 static int FrontendInfo( input_thread_t * p_input )
258 thread_dvb_data_t * p_dvb
259 = (thread_dvb_data_t *)p_input->p_access_data;
260 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
263 /* Determine type of frontend */
264 if ( (i_ret = ioctl( p_frontend->i_handle, FE_GET_INFO,
265 &p_frontend->info )) < 0 )
267 msg_Err( p_input, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
272 /* Print out frontend capabilities. */
273 msg_Dbg(p_input, "Frontend Info:" );
274 msg_Dbg(p_input, " name = %s", p_frontend->info.name);
275 switch ( p_frontend->info.type )
278 msg_Dbg( p_input, " type = QPSK (DVB-S)" );
281 msg_Dbg( p_input, " type = QAM (DVB-C)" );
284 msg_Dbg( p_input, " type = OFDM (DVB-T)" );
286 #if 0 /* DVB_API_VERSION == 3 */
288 msg_Dbg(p_input, " type = MEMORY" );
291 msg_Dbg(p_input, " type = NETWORK" );
295 msg_Err( p_input, " unknown frontend type (%d)",
296 p_frontend->info.type );
299 msg_Dbg(p_input, " frequency_min = %u (kHz)",
300 p_frontend->info.frequency_min);
301 msg_Dbg(p_input, " frequency_max = %u (kHz)",
302 p_frontend->info.frequency_max);
303 msg_Dbg(p_input, " frequency_stepsize = %u",
304 p_frontend->info.frequency_stepsize);
305 msg_Dbg(p_input, " frequency_tolerance = %u",
306 p_frontend->info.frequency_tolerance);
307 msg_Dbg(p_input, " symbol_rate_min = %u (kHz)",
308 p_frontend->info.symbol_rate_min);
309 msg_Dbg(p_input, " symbol_rate_max = %u (kHz)",
310 p_frontend->info.symbol_rate_max);
311 msg_Dbg(p_input, " symbol_rate_tolerance (ppm) = %u",
312 p_frontend->info.symbol_rate_tolerance);
313 msg_Dbg(p_input, " notifier_delay (ms) = %u",
314 p_frontend->info.notifier_delay );
316 msg_Dbg(p_input, "Frontend Info capability list:");
317 if (p_frontend->info.caps & FE_IS_STUPID)
318 msg_Dbg(p_input, " no capabilities - frontend is stupid!");
319 if (p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
320 msg_Dbg(p_input, " inversion auto");
321 if (p_frontend->info.caps & FE_CAN_FEC_1_2)
322 msg_Dbg(p_input, " forward error correction 1/2");
323 if (p_frontend->info.caps & FE_CAN_FEC_2_3)
324 msg_Dbg(p_input, " forward error correction 2/3");
325 if (p_frontend->info.caps & FE_CAN_FEC_3_4)
326 msg_Dbg(p_input, " forward error correction 3/4");
327 if (p_frontend->info.caps & FE_CAN_FEC_4_5)
328 msg_Dbg(p_input, " forward error correction 4/5");
329 if (p_frontend->info.caps & FE_CAN_FEC_5_6)
330 msg_Dbg(p_input, " forward error correction 5/6");
331 if (p_frontend->info.caps & FE_CAN_FEC_6_7)
332 msg_Dbg(p_input, " forward error correction 6/7");
333 if (p_frontend->info.caps & FE_CAN_FEC_7_8)
334 msg_Dbg(p_input, " forward error correction 7/8");
335 if (p_frontend->info.caps & FE_CAN_FEC_8_9)
336 msg_Dbg(p_input, " forward error correction 8/9");
337 if (p_frontend->info.caps & FE_CAN_FEC_AUTO)
338 msg_Dbg(p_input, " forward error correction auto");
339 if (p_frontend->info.caps & FE_CAN_QPSK)
340 msg_Dbg(p_input, " card can do QPSK");
341 if (p_frontend->info.caps & FE_CAN_QAM_16)
342 msg_Dbg(p_input, " card can do QAM 16");
343 if (p_frontend->info.caps & FE_CAN_QAM_32)
344 msg_Dbg(p_input, " card can do QAM 32");
345 if (p_frontend->info.caps & FE_CAN_QAM_64)
346 msg_Dbg(p_input, " card can do QAM 64");
347 if (p_frontend->info.caps & FE_CAN_QAM_128)
348 msg_Dbg(p_input, " card can do QAM 128");
349 if (p_frontend->info.caps & FE_CAN_QAM_256)
350 msg_Dbg(p_input, " card can do QAM 256");
351 if (p_frontend->info.caps & FE_CAN_QAM_AUTO)
352 msg_Dbg(p_input, " card can do QAM auto");
353 if (p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
354 msg_Dbg(p_input, " transmission mode auto");
355 if (p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
356 msg_Dbg(p_input, " bandwidth mode auto");
357 if (p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
358 msg_Dbg(p_input, " guard interval mode auto");
359 if (p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
360 msg_Dbg(p_input, " hierarchy mode auto");
361 if (p_frontend->info.caps & FE_CAN_MUTE_TS)
362 msg_Dbg(p_input, " card can mute TS");
363 if (p_frontend->info.caps & FE_CAN_CLEAN_SETUP)
364 msg_Dbg(p_input, " clean setup");
365 msg_Dbg(p_input, "End of capability list");
370 /*****************************************************************************
371 * Decoding the DVB parameters (common)
372 *****************************************************************************/
373 static fe_spectral_inversion_t DecodeInversion( input_thread_t * p_input )
376 fe_spectral_inversion_t fe_inversion = 0;
378 var_Get( p_input, "dvb-inversion", &val );
379 msg_Dbg( p_input, "using inversion=%d", val.i_int );
383 case 0: fe_inversion = INVERSION_OFF; break;
384 case 1: fe_inversion = INVERSION_ON; break;
385 case 2: fe_inversion = INVERSION_AUTO; break;
387 msg_Dbg( p_input, "dvb has inversion not set, using auto");
388 fe_inversion = INVERSION_AUTO;
394 static fe_code_rate_t DecodeFEC( input_thread_t * p_input, int i_val )
396 fe_code_rate_t fe_fec = FEC_NONE;
398 msg_Dbg( p_input, "using feq=%d", i_val );
402 case 1: fe_fec = FEC_1_2; break;
403 case 2: fe_fec = FEC_2_3; break;
404 case 3: fe_fec = FEC_3_4; break;
405 case 4: fe_fec = FEC_4_5; break;
406 case 5: fe_fec = FEC_5_6; break;
407 case 6: fe_fec = FEC_6_7; break;
408 case 7: fe_fec = FEC_7_8; break;
409 case 8: fe_fec = FEC_8_9; break;
410 case 9: fe_fec = FEC_AUTO; break;
414 msg_Err( p_input, "argument has invalid FEC (%d)", i_val);
420 static fe_modulation_t DecodeModulation( input_thread_t * p_input )
423 fe_modulation_t fe_modulation = 0;
425 var_Get( p_input, "dvb-modulation", &val );
429 case -1: fe_modulation = QPSK; break;
430 case 0: fe_modulation = QAM_AUTO; break;
431 case 16: fe_modulation = QAM_16; break;
432 case 32: fe_modulation = QAM_32; break;
433 case 64: fe_modulation = QAM_64; break;
434 case 128: fe_modulation = QAM_128; break;
435 case 256: fe_modulation = QAM_256; break;
437 msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
438 fe_modulation = QAM_AUTO;
441 return fe_modulation;
444 /*****************************************************************************
445 * FrontendSetQPSK : controls the FE device
446 *****************************************************************************/
447 static fe_sec_voltage_t DecodeVoltage( input_thread_t * p_input )
450 fe_sec_voltage_t fe_voltage;
452 var_Get( p_input, "dvb-voltage", &val );
453 msg_Dbg( p_input, "using voltage=%d", val.i_int );
457 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
458 case 13: fe_voltage = SEC_VOLTAGE_13; break;
459 case 18: fe_voltage = SEC_VOLTAGE_18; break;
461 fe_voltage = SEC_VOLTAGE_OFF;
462 msg_Err( p_input, "argument has invalid voltage (%d)", val.i_int);
468 static fe_sec_tone_mode_t DecodeTone( input_thread_t * p_input )
471 fe_sec_tone_mode_t fe_tone;
473 var_Get( p_input, "dvb-tone", &val );
474 msg_Dbg( p_input, "using tone=%d", val.i_int );
478 case 0: fe_tone = SEC_TONE_OFF; break;
479 case 1: fe_tone = SEC_TONE_ON; break;
481 fe_tone = SEC_TONE_OFF;
482 msg_Err( p_input, "argument has invalid tone mode (%d)", val.i_int);
490 struct dvb_diseqc_master_cmd cmd;
494 static int DoDiseqc( input_thread_t * p_input )
496 thread_dvb_data_t * p_dvb
497 = (thread_dvb_data_t *)p_input->p_access_data;
498 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
500 int i_frequency, i_lnb_slof;
501 fe_sec_voltage_t fe_voltage;
502 fe_sec_tone_mode_t fe_tone;
505 var_Get( p_input, "dvb-frequency", &val );
506 i_frequency = val.i_int;
507 var_Get( p_input, "dvb-lnb-slof", &val );
508 i_lnb_slof = val.i_int;
510 var_Get( p_input, "dvb-tone", &val );
511 if ( val.i_int == -1 /* auto */ )
513 if ( i_frequency >= i_lnb_slof )
517 var_Set( p_input, "dvb-tone", val );
520 fe_voltage = DecodeVoltage( p_input );
521 fe_tone = DecodeTone( p_input );
523 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
525 msg_Err( p_input, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
526 fe_voltage, i_err, strerror(errno) );
530 var_Get( p_input, "dvb-satno", &val );
531 if ( val.i_int != 0 )
533 /* digital satellite equipment control,
534 * specification is available from http://www.eutelsat.com/
536 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
537 SEC_TONE_OFF )) < 0 )
539 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
540 i_err, strerror(errno) );
546 if ( val.i_int >= 1 && val.i_int <= 4 )
548 /* 1.x compatible equipment */
549 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
551 /* param: high nibble: reset bits, low nibble set bits,
552 * bits are: option, position, polarization, band
554 cmd.cmd.msg[3] = 0xf0 /* reset bits */
555 | (((val.i_int - 1) * 4) & 0xc)
556 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
557 | (fe_tone == SEC_TONE_ON ? 1 : 0);
559 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
562 msg_Err( p_input, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
563 i_err, strerror(errno) );
567 msleep(cmd.wait * 1000);
571 /* A or B simple diseqc */
572 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
573 val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
575 msg_Err( p_input, "ioctl FE_SEND_BURST failed (%d) %s",
576 i_err, strerror(errno) );
584 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
586 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
587 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
596 static int FrontendSetQPSK( input_thread_t * p_input )
598 thread_dvb_data_t * p_dvb
599 = (thread_dvb_data_t *)p_input->p_access_data;
600 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
601 struct dvb_frontend_parameters fep;
604 int i_frequency, i_lnb_slof;
606 /* Prepare the fep structure */
608 var_Get( p_input, "dvb-frequency", &val );
609 i_frequency = val.i_int;
610 var_Get( p_input, "dvb-lnb-slof", &val );
611 i_lnb_slof = val.i_int;
613 if ( i_frequency >= i_lnb_slof )
614 var_Get( p_input, "dvb-lnb-lof2", &val );
616 var_Get( p_input, "dvb-lnb-lof1", &val );
617 fep.frequency = i_frequency - val.i_int;
619 fep.inversion = DecodeInversion( p_input );
621 var_Get( p_input, "dvb-srate", &val );
622 fep.u.qpsk.symbol_rate = val.i_int;
624 var_Get( p_input, "dvb-fec", &val );
625 fep.u.qpsk.fec_inner = DecodeFEC( p_input, val.i_int );
627 if ( DoDiseqc( p_input ) < 0 )
633 /* Empty the event queue */
636 struct dvb_frontend_event event;
637 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
641 /* Now send it all to the frontend device */
642 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
644 msg_Err( p_input, "DVB-S: setting frontend failed (%d) %s", i_ret,
649 return FrontendCheck( p_input );
652 /*****************************************************************************
653 * FrontendSetQAM : controls the FE device
654 *****************************************************************************/
655 static int FrontendSetQAM( input_thread_t * p_input )
657 thread_dvb_data_t * p_dvb
658 = (thread_dvb_data_t *)p_input->p_access_data;
659 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
660 struct dvb_frontend_parameters fep;
664 /* Prepare the fep structure */
666 var_Get( p_input, "dvb-frequency", &val );
667 fep.frequency = val.i_int;
669 fep.inversion = DecodeInversion( p_input );
671 var_Get( p_input, "dvb-srate", &val );
672 fep.u.qam.symbol_rate = val.i_int;
674 var_Get( p_input, "dvb-fec", &val );
675 fep.u.qam.fec_inner = DecodeFEC( p_input, val.i_int );
677 fep.u.qam.modulation = DecodeModulation( p_input );
679 /* Now send it all to the frontend device */
680 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
682 msg_Err( p_input, "DVB-C: setting frontend failed (%d) %s", i_ret,
687 return FrontendCheck( p_input );
690 /*****************************************************************************
691 * FrontendSetOFDM : controls the FE device
692 *****************************************************************************/
693 static fe_bandwidth_t DecodeBandwidth( input_thread_t * p_input )
696 fe_bandwidth_t fe_bandwidth = 0;
698 var_Get( p_input, "dvb-bandwidth", &val );
699 msg_Dbg( p_input, "using bandwidth=%d", val.i_int );
703 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
704 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
705 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
706 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
708 msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto" );
709 fe_bandwidth = BANDWIDTH_AUTO;
715 static fe_transmit_mode_t DecodeTransmission( input_thread_t * p_input )
718 fe_transmit_mode_t fe_transmission = 0;
720 var_Get( p_input, "dvb-transmission", &val );
721 msg_Dbg( p_input, "using transmission=%d", val.i_int );
725 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
726 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
727 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
729 msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
730 fe_transmission = TRANSMISSION_MODE_AUTO;
733 return fe_transmission;
736 static fe_guard_interval_t DecodeGuardInterval( input_thread_t * p_input )
739 fe_guard_interval_t fe_guard = 0;
741 var_Get( p_input, "dvb-guard", &val );
742 msg_Dbg( p_input, "using guard=%d", val.i_int );
746 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
747 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
748 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
749 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
750 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
752 msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
753 fe_guard = GUARD_INTERVAL_AUTO;
759 static fe_hierarchy_t DecodeHierarchy( input_thread_t * p_input )
762 fe_hierarchy_t fe_hierarchy = 0;
764 var_Get( p_input, "dvb-hierarchy", &val );
765 msg_Dbg( p_input, "using hierarchy=%d", val.i_int );
769 case -1: fe_hierarchy = HIERARCHY_NONE; break;
770 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
771 case 1: fe_hierarchy = HIERARCHY_1; break;
772 case 2: fe_hierarchy = HIERARCHY_2; break;
773 case 4: fe_hierarchy = HIERARCHY_4; break;
775 msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
776 fe_hierarchy = HIERARCHY_AUTO;
782 static int FrontendSetOFDM( input_thread_t * p_input )
784 thread_dvb_data_t * p_dvb
785 = (thread_dvb_data_t *)p_input->p_access_data;
786 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
787 struct dvb_frontend_parameters fep;
791 /* Prepare the fep structure */
793 var_Get( p_input, "dvb-frequency", &val );
794 fep.frequency = val.i_int;
796 fep.inversion = DecodeInversion( p_input );
798 fep.u.ofdm.bandwidth = DecodeBandwidth( p_input );
799 var_Get( p_input, "dvb-code-rate-HP", &val );
800 fep.u.ofdm.code_rate_HP = DecodeFEC( p_input, val.i_int );
801 var_Get( p_input, "dvb-code-rate-LP", &val );
802 fep.u.ofdm.code_rate_LP = DecodeFEC( p_input, val.i_int );
803 fep.u.ofdm.constellation = DecodeModulation( p_input );
804 fep.u.ofdm.transmission_mode = DecodeTransmission( p_input );
805 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_input );
806 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_input );
808 /* Now send it all to the frontend device */
809 if ( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
811 msg_Err( p_input, "DVB-T: setting frontend failed (%d) %s", ret,
816 return FrontendCheck( p_input );
819 /******************************************************************
820 * FrontendCheck: Check completion of the frontend control sequence
821 ******************************************************************/
822 static int FrontendCheck( input_thread_t * p_input )
824 thread_dvb_data_t * p_dvb
825 = (thread_dvb_data_t *)p_input->p_access_data;
826 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
829 while ( !p_input->b_die && !p_input->b_error )
832 if ( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
835 msg_Err( p_input, "reading frontend status failed (%d) %s",
836 i_ret, strerror(errno) );
840 if (status & FE_HAS_SIGNAL) /* found something above the noise level */
841 msg_Dbg(p_input, "check frontend ... has signal");
843 if (status & FE_HAS_CARRIER) /* found a DVB signal */
844 msg_Dbg(p_input, "check frontend ... has carrier");
846 if (status & FE_HAS_VITERBI) /* FEC is stable */
847 msg_Dbg(p_input, "check frontend ... has stable forward error correction");
849 if (status & FE_HAS_SYNC) /* found sync bytes */
850 msg_Dbg(p_input, "check frontend ... has sync");
852 if (status & FE_HAS_LOCK) /* everything's working... */
855 msg_Dbg(p_input, "check frontend ... has lock");
856 msg_Dbg(p_input, "tuning succeeded");
858 /* Read some statistics */
860 if ( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
861 msg_Dbg( p_input, "Bit error rate: %d", value );
864 if ( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
865 msg_Dbg( p_input, "Signal strength: %d", value );
868 if ( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
869 msg_Dbg( p_input, "SNR: %d", value );
874 if (status & FE_TIMEDOUT) /* no lock within the last ~2 seconds */
876 msg_Err(p_input, "tuning failed ... timed out");
880 if (status & FE_REINIT)
882 /* frontend was reinitialized, */
883 /* application is recommended to reset */
884 /* DiSEqC, tone and parameters */
885 msg_Err(p_input, "tuning failed ... resend frontend parameters");
899 /*****************************************************************************
900 * DMXSetFilter : controls the demux to add a filter
901 *****************************************************************************/
902 int E_(DMXSetFilter)( input_thread_t * p_input, int i_pid, int * pi_fd,
905 struct dmx_pes_filter_params s_filter_params;
907 unsigned int i_adapter, i_device;
911 var_Get( p_input, "dvb-adapter", &val );
912 i_adapter = val.i_int;
913 var_Get( p_input, "dvb-device", &val );
914 i_device = val.i_int;
916 if ( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
917 >= (int)sizeof(dmx) )
919 msg_Err( p_input, "snprintf() truncated string for DMX" );
920 dmx[sizeof(dmx) - 1] = '\0';
923 msg_Dbg( p_input, "Opening device %s", dmx );
924 if ( (*pi_fd = open(dmx, O_RDWR)) < 0 )
926 msg_Err( p_input, "DMXSetFilter: opening device failed (%s)",
931 /* We fill the DEMUX structure : */
932 s_filter_params.pid = i_pid;
933 s_filter_params.input = DMX_IN_FRONTEND;
934 s_filter_params.output = DMX_OUT_TS_TAP;
935 s_filter_params.flags = DMX_IMMEDIATE_START;
940 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
941 s_filter_params.pes_type = DMX_PES_VIDEO0;
944 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
945 s_filter_params.pes_type = DMX_PES_AUDIO0;
948 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
949 s_filter_params.pes_type = DMX_PES_TELETEXT0;
952 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
953 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
956 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
957 s_filter_params.pes_type = DMX_PES_PCR0;
961 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
962 s_filter_params.pes_type = DMX_PES_VIDEO1;
965 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
966 s_filter_params.pes_type = DMX_PES_AUDIO1;
969 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
970 s_filter_params.pes_type = DMX_PES_TELETEXT1;
973 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
974 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
977 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
978 s_filter_params.pes_type = DMX_PES_PCR1;
982 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
983 s_filter_params.pes_type = DMX_PES_VIDEO2;
986 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
987 s_filter_params.pes_type = DMX_PES_AUDIO2;
990 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
991 s_filter_params.pes_type = DMX_PES_TELETEXT2;
994 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
995 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
998 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
999 s_filter_params.pes_type = DMX_PES_PCR2;
1003 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1004 s_filter_params.pes_type = DMX_PES_VIDEO3;
1007 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1008 s_filter_params.pes_type = DMX_PES_AUDIO3;
1011 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1012 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1015 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1016 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1019 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1020 s_filter_params.pes_type = DMX_PES_PCR3;
1022 /* Usually used by Nova cards */
1025 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1026 s_filter_params.pes_type = DMX_PES_OTHER;
1030 /* We then give the order to the device : */
1031 if ( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1033 msg_Err( p_input, "DMXSetFilter: failed with %d (%s)", i_ret,
1040 /*****************************************************************************
1041 * DMXUnsetFilter : removes a filter
1042 *****************************************************************************/
1043 int E_(DMXUnsetFilter)( input_thread_t * p_input, int i_fd )
1047 if ( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1049 msg_Err( p_input, "DMX_STOP failed for demux (%d) %s",
1050 i_ret, strerror(errno) );
1054 msg_Dbg( p_input, "DMXUnsetFilter: closing demux %d", i_fd);
1064 /*****************************************************************************
1066 *****************************************************************************/
1067 int E_(DVROpen)( input_thread_t * p_input )
1069 thread_dvb_data_t * p_dvb
1070 = (thread_dvb_data_t *)p_input->p_access_data;
1071 unsigned int i_adapter, i_device;
1075 var_Get( p_input, "dvb-adapter", &val );
1076 i_adapter = val.i_int;
1077 var_Get( p_input, "dvb-device", &val );
1078 i_device = val.i_int;
1080 if ( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1081 >= (int)sizeof(dvr) )
1083 msg_Err( p_input, "snprintf() truncated string for DVR" );
1084 dvr[sizeof(dvr) - 1] = '\0';
1087 msg_Dbg( p_input, "Opening device %s", dvr );
1088 if ( (p_dvb->i_handle = open(dvr, O_RDONLY)) < 0 )
1090 msg_Err( p_input, "DVROpen: opening device failed (%s)",
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 void E_(DVRClose)( input_thread_t * p_input )
1103 thread_dvb_data_t * p_dvb
1104 = (thread_dvb_data_t *)p_input->p_access_data;
1106 close( p_dvb->i_handle );