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 = (frontend_t *) malloc(sizeof(frontend_t));
103 if( p_frontend == NULL )
105 msg_Err( p_input, "FrontEndOpen: out of memory" );
109 p_dvb->p_frontend = p_frontend;
111 msg_Dbg( p_input, "Opening device %s", frontend );
112 if ( (p_frontend->i_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
114 msg_Err( p_input, "FrontEndOpen: opening device failed (%s)",
122 char * psz_expected = NULL;
125 if ( FrontendInfo( p_input ) < 0 )
127 close( p_frontend->i_handle );
132 switch ( p_frontend->info.type )
144 psz_real = "unknown";
148 if( ((strncmp( p_input->psz_access, "qpsk", 4 ) == 0) ||
149 (strncmp( p_input->psz_access, "dvb-s", 5 ) == 0) ||
150 (strncmp( p_input->psz_access, "satellite", 9 ) == 0) ) &&
151 (p_frontend->info.type != FE_QPSK) )
153 psz_expected = "DVB-S";
155 if( ((strncmp( p_input->psz_access, "cable", 5 ) == 0) ||
156 (strncmp( p_input->psz_access, "dvb-c", 5 ) == 0) ) &&
157 (p_frontend->info.type != FE_QAM) )
159 psz_expected = "DVB-C";
161 if( ((strncmp( p_input->psz_access, "terrestrial", 11 ) == 0) ||
162 (strncmp( p_input->psz_access, "dvb-t", 5 ) == 0) ) &&
163 (p_frontend->info.type != FE_OFDM) )
165 psz_expected = "DVB-T";
168 if ( psz_expected != NULL )
170 msg_Err( p_input, "the user asked for %s, and the tuner is %s",
171 psz_expected, psz_real );
172 close( p_frontend->i_handle );
177 else /* no frontend probing is done so use default border values. */
179 msg_Dbg( p_input, "using default values for frontend info" );
181 msg_Dbg( p_input, "method of access is %s", p_input->psz_access );
182 p_frontend->info.type = FE_QPSK;
183 if ( !strncmp( p_input->psz_access, "qpsk", 4 ) ||
184 !strncmp( p_input->psz_access, "dvb-s", 5 ) )
185 p_frontend->info.type = FE_QPSK;
186 else if ( !strncmp( p_input->psz_access, "cable", 5 ) ||
187 !strncmp( p_input->psz_access, "dvb-c", 5 ) )
188 p_frontend->info.type = FE_QAM;
189 else if ( !strncmp( p_input->psz_access, "terrestrial", 11 ) ||
190 !strncmp( p_input->psz_access, "dvb-t", 5 ) )
191 p_frontend->info.type = FE_OFDM;
197 /*****************************************************************************
198 * FrontendClose : Close the frontend
199 *****************************************************************************/
200 void E_(FrontendClose)( input_thread_t * p_input )
202 thread_dvb_data_t * p_dvb
203 = (thread_dvb_data_t *)p_input->p_access_data;
204 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
206 if ( p_frontend != NULL )
208 close( p_frontend->i_handle );
213 /*****************************************************************************
214 * FrontendSet : Tune !
215 *****************************************************************************/
216 int E_(FrontendSet)( input_thread_t * p_input )
218 thread_dvb_data_t * p_dvb
219 = (thread_dvb_data_t *)p_input->p_access_data;
220 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
222 switch ( p_frontend->info.type )
224 /* DVB-S: satellite and budget cards (nova) */
226 if ( FrontendSetQPSK( p_input ) < 0 )
228 msg_Err( p_input, "DVB-S: tuning failed" );
235 if ( FrontendSetQAM( p_input ) < 0 )
237 msg_Err( p_input, "DVB-C: tuning failed" );
244 if ( FrontendSetOFDM( p_input ) < 0 )
246 msg_Err( p_input, "DVB-T: tuning failed" );
252 msg_Err( p_input, "Could not determine frontend type on %s",
253 p_frontend->info.name );
259 /*****************************************************************************
260 * FrontendInfo : Return information about given frontend
261 *****************************************************************************/
262 static int FrontendInfo( input_thread_t * p_input )
264 thread_dvb_data_t * p_dvb
265 = (thread_dvb_data_t *)p_input->p_access_data;
266 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
269 /* Determine type of frontend */
270 if ( (i_ret = ioctl( p_frontend->i_handle, FE_GET_INFO,
271 &p_frontend->info )) < 0 )
273 msg_Err( p_input, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
278 /* Print out frontend capabilities. */
279 msg_Dbg(p_input, "Frontend Info:" );
280 msg_Dbg(p_input, " name = %s", p_frontend->info.name);
281 switch ( p_frontend->info.type )
284 msg_Dbg( p_input, " type = QPSK (DVB-S)" );
287 msg_Dbg( p_input, " type = QAM (DVB-C)" );
290 msg_Dbg( p_input, " type = OFDM (DVB-T)" );
292 #if 0 /* DVB_API_VERSION == 3 */
294 msg_Dbg(p_input, " type = MEMORY" );
297 msg_Dbg(p_input, " type = NETWORK" );
301 msg_Err( p_input, " unknown frontend type (%d)",
302 p_frontend->info.type );
305 msg_Dbg(p_input, " frequency_min = %u (kHz)",
306 p_frontend->info.frequency_min);
307 msg_Dbg(p_input, " frequency_max = %u (kHz)",
308 p_frontend->info.frequency_max);
309 msg_Dbg(p_input, " frequency_stepsize = %u",
310 p_frontend->info.frequency_stepsize);
311 msg_Dbg(p_input, " frequency_tolerance = %u",
312 p_frontend->info.frequency_tolerance);
313 msg_Dbg(p_input, " symbol_rate_min = %u (kHz)",
314 p_frontend->info.symbol_rate_min);
315 msg_Dbg(p_input, " symbol_rate_max = %u (kHz)",
316 p_frontend->info.symbol_rate_max);
317 msg_Dbg(p_input, " symbol_rate_tolerance (ppm) = %u",
318 p_frontend->info.symbol_rate_tolerance);
319 msg_Dbg(p_input, " notifier_delay (ms) = %u",
320 p_frontend->info.notifier_delay );
322 msg_Dbg(p_input, "Frontend Info capability list:");
323 if (p_frontend->info.caps & FE_IS_STUPID)
324 msg_Dbg(p_input, " no capabilities - frontend is stupid!");
325 if (p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
326 msg_Dbg(p_input, " inversion auto");
327 if (p_frontend->info.caps & FE_CAN_FEC_1_2)
328 msg_Dbg(p_input, " forward error correction 1/2");
329 if (p_frontend->info.caps & FE_CAN_FEC_2_3)
330 msg_Dbg(p_input, " forward error correction 2/3");
331 if (p_frontend->info.caps & FE_CAN_FEC_3_4)
332 msg_Dbg(p_input, " forward error correction 3/4");
333 if (p_frontend->info.caps & FE_CAN_FEC_4_5)
334 msg_Dbg(p_input, " forward error correction 4/5");
335 if (p_frontend->info.caps & FE_CAN_FEC_5_6)
336 msg_Dbg(p_input, " forward error correction 5/6");
337 if (p_frontend->info.caps & FE_CAN_FEC_6_7)
338 msg_Dbg(p_input, " forward error correction 6/7");
339 if (p_frontend->info.caps & FE_CAN_FEC_7_8)
340 msg_Dbg(p_input, " forward error correction 7/8");
341 if (p_frontend->info.caps & FE_CAN_FEC_8_9)
342 msg_Dbg(p_input, " forward error correction 8/9");
343 if (p_frontend->info.caps & FE_CAN_FEC_AUTO)
344 msg_Dbg(p_input, " forward error correction auto");
345 if (p_frontend->info.caps & FE_CAN_QPSK)
346 msg_Dbg(p_input, " card can do QPSK");
347 if (p_frontend->info.caps & FE_CAN_QAM_16)
348 msg_Dbg(p_input, " card can do QAM 16");
349 if (p_frontend->info.caps & FE_CAN_QAM_32)
350 msg_Dbg(p_input, " card can do QAM 32");
351 if (p_frontend->info.caps & FE_CAN_QAM_64)
352 msg_Dbg(p_input, " card can do QAM 64");
353 if (p_frontend->info.caps & FE_CAN_QAM_128)
354 msg_Dbg(p_input, " card can do QAM 128");
355 if (p_frontend->info.caps & FE_CAN_QAM_256)
356 msg_Dbg(p_input, " card can do QAM 256");
357 if (p_frontend->info.caps & FE_CAN_QAM_AUTO)
358 msg_Dbg(p_input, " card can do QAM auto");
359 if (p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
360 msg_Dbg(p_input, " transmission mode auto");
361 if (p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
362 msg_Dbg(p_input, " bandwidth mode auto");
363 if (p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
364 msg_Dbg(p_input, " guard interval mode auto");
365 if (p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
366 msg_Dbg(p_input, " hierarchy mode auto");
367 if (p_frontend->info.caps & FE_CAN_MUTE_TS)
368 msg_Dbg(p_input, " card can mute TS");
369 if (p_frontend->info.caps & FE_CAN_CLEAN_SETUP)
370 msg_Dbg(p_input, " clean setup");
371 msg_Dbg(p_input, "End of capability list");
376 /*****************************************************************************
377 * Decoding the DVB parameters (common)
378 *****************************************************************************/
379 static fe_spectral_inversion_t DecodeInversion( input_thread_t * p_input )
382 fe_spectral_inversion_t fe_inversion = 0;
384 var_Get( p_input, "dvb-inversion", &val );
385 msg_Dbg( p_input, "using inversion=%d", val.i_int );
389 case 0: fe_inversion = INVERSION_OFF; break;
390 case 1: fe_inversion = INVERSION_ON; break;
391 case 2: fe_inversion = INVERSION_AUTO; break;
393 msg_Dbg( p_input, "dvb has inversion not set, using auto");
394 fe_inversion = INVERSION_AUTO;
400 static fe_code_rate_t DecodeFEC( input_thread_t * p_input, int i_val )
402 fe_code_rate_t fe_fec = FEC_NONE;
404 msg_Dbg( p_input, "using fec=%d", i_val );
408 case 0: fe_fec = FEC_NONE; break;
409 case 1: fe_fec = FEC_1_2; break;
410 case 2: fe_fec = FEC_2_3; break;
411 case 3: fe_fec = FEC_3_4; break;
412 case 4: fe_fec = FEC_4_5; break;
413 case 5: fe_fec = FEC_5_6; break;
414 case 6: fe_fec = FEC_6_7; break;
415 case 7: fe_fec = FEC_7_8; break;
416 case 8: fe_fec = FEC_8_9; break;
417 case 9: fe_fec = FEC_AUTO; break;
421 msg_Err( p_input, "argument has invalid FEC (%d)", i_val);
427 static fe_modulation_t DecodeModulation( input_thread_t * p_input )
430 fe_modulation_t fe_modulation = 0;
432 var_Get( p_input, "dvb-modulation", &val );
436 case -1: fe_modulation = QPSK; break;
437 case 0: fe_modulation = QAM_AUTO; break;
438 case 16: fe_modulation = QAM_16; break;
439 case 32: fe_modulation = QAM_32; break;
440 case 64: fe_modulation = QAM_64; break;
441 case 128: fe_modulation = QAM_128; break;
442 case 256: fe_modulation = QAM_256; break;
444 msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
445 fe_modulation = QAM_AUTO;
448 return fe_modulation;
451 /*****************************************************************************
452 * FrontendSetQPSK : controls the FE device
453 *****************************************************************************/
454 static fe_sec_voltage_t DecodeVoltage( input_thread_t * p_input )
457 fe_sec_voltage_t fe_voltage;
459 var_Get( p_input, "dvb-voltage", &val );
460 msg_Dbg( p_input, "using voltage=%d", val.i_int );
464 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
465 case 13: fe_voltage = SEC_VOLTAGE_13; break;
466 case 18: fe_voltage = SEC_VOLTAGE_18; break;
468 fe_voltage = SEC_VOLTAGE_OFF;
469 msg_Err( p_input, "argument has invalid voltage (%d)", val.i_int);
475 static fe_sec_tone_mode_t DecodeTone( input_thread_t * p_input )
478 fe_sec_tone_mode_t fe_tone;
480 var_Get( p_input, "dvb-tone", &val );
481 msg_Dbg( p_input, "using tone=%d", val.i_int );
485 case 0: fe_tone = SEC_TONE_OFF; break;
486 case 1: fe_tone = SEC_TONE_ON; break;
488 fe_tone = SEC_TONE_OFF;
489 msg_Err( p_input, "argument has invalid tone mode (%d)", val.i_int);
497 struct dvb_diseqc_master_cmd cmd;
501 static int DoDiseqc( input_thread_t * p_input )
503 thread_dvb_data_t * p_dvb
504 = (thread_dvb_data_t *)p_input->p_access_data;
505 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
507 int i_frequency, i_lnb_slof;
508 fe_sec_voltage_t fe_voltage;
509 fe_sec_tone_mode_t fe_tone;
512 var_Get( p_input, "dvb-frequency", &val );
513 i_frequency = val.i_int;
514 var_Get( p_input, "dvb-lnb-slof", &val );
515 i_lnb_slof = val.i_int;
517 var_Get( p_input, "dvb-tone", &val );
518 if ( val.i_int == -1 /* auto */ )
520 if ( i_frequency >= i_lnb_slof )
524 var_Set( p_input, "dvb-tone", val );
527 fe_voltage = DecodeVoltage( p_input );
528 fe_tone = DecodeTone( p_input );
530 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
532 msg_Err( p_input, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
533 fe_voltage, i_err, strerror(errno) );
537 var_Get( p_input, "dvb-satno", &val );
538 if ( val.i_int != 0 )
540 /* digital satellite equipment control,
541 * specification is available from http://www.eutelsat.com/
543 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
544 SEC_TONE_OFF )) < 0 )
546 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
547 i_err, strerror(errno) );
553 if ( val.i_int >= 1 && val.i_int <= 4 )
555 /* 1.x compatible equipment */
556 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
558 /* param: high nibble: reset bits, low nibble set bits,
559 * bits are: option, position, polarization, band
561 cmd.cmd.msg[3] = 0xf0 /* reset bits */
562 | (((val.i_int - 1) * 4) & 0xc)
563 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
564 | (fe_tone == SEC_TONE_ON ? 1 : 0);
566 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
569 msg_Err( p_input, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
570 i_err, strerror(errno) );
574 msleep(cmd.wait * 1000);
578 /* A or B simple diseqc */
579 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
580 val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
582 msg_Err( p_input, "ioctl FE_SEND_BURST failed (%d) %s",
583 i_err, strerror(errno) );
591 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
593 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
594 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
603 static int FrontendSetQPSK( input_thread_t * p_input )
605 thread_dvb_data_t * p_dvb
606 = (thread_dvb_data_t *)p_input->p_access_data;
607 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
608 struct dvb_frontend_parameters fep;
611 int i_frequency, i_lnb_slof;
613 /* Prepare the fep structure */
615 var_Get( p_input, "dvb-frequency", &val );
616 i_frequency = val.i_int;
617 var_Get( p_input, "dvb-lnb-slof", &val );
618 i_lnb_slof = val.i_int;
620 if ( i_frequency >= i_lnb_slof )
621 var_Get( p_input, "dvb-lnb-lof2", &val );
623 var_Get( p_input, "dvb-lnb-lof1", &val );
624 fep.frequency = i_frequency - val.i_int;
626 fep.inversion = DecodeInversion( p_input );
628 var_Get( p_input, "dvb-srate", &val );
629 fep.u.qpsk.symbol_rate = val.i_int;
631 var_Get( p_input, "dvb-fec", &val );
632 fep.u.qpsk.fec_inner = DecodeFEC( p_input, val.i_int );
634 if ( DoDiseqc( p_input ) < 0 )
641 /* Empty the event queue */
644 struct dvb_frontend_event event;
645 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
649 /* Now send it all to the frontend device */
650 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
652 msg_Err( p_input, "DVB-S: setting frontend failed (%d) %s", i_ret,
657 return FrontendCheck( p_input );
660 /*****************************************************************************
661 * FrontendSetQAM : controls the FE device
662 *****************************************************************************/
663 static int FrontendSetQAM( input_thread_t * p_input )
665 thread_dvb_data_t * p_dvb
666 = (thread_dvb_data_t *)p_input->p_access_data;
667 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
668 struct dvb_frontend_parameters fep;
672 /* Prepare the fep structure */
674 var_Get( p_input, "dvb-frequency", &val );
675 fep.frequency = val.i_int;
677 fep.inversion = DecodeInversion( p_input );
679 var_Get( p_input, "dvb-srate", &val );
680 fep.u.qam.symbol_rate = val.i_int;
682 var_Get( p_input, "dvb-fec", &val );
683 fep.u.qam.fec_inner = DecodeFEC( p_input, val.i_int );
685 fep.u.qam.modulation = DecodeModulation( p_input );
687 /* Empty the event queue */
690 struct dvb_frontend_event event;
691 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
695 /* Now send it all to the frontend device */
696 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
698 msg_Err( p_input, "DVB-C: setting frontend failed (%d) %s", i_ret,
703 return FrontendCheck( p_input );
706 /*****************************************************************************
707 * FrontendSetOFDM : controls the FE device
708 *****************************************************************************/
709 static fe_bandwidth_t DecodeBandwidth( input_thread_t * p_input )
712 fe_bandwidth_t fe_bandwidth = 0;
714 var_Get( p_input, "dvb-bandwidth", &val );
715 msg_Dbg( p_input, "using bandwidth=%d", val.i_int );
719 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
720 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
721 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
722 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
724 msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto" );
725 fe_bandwidth = BANDWIDTH_AUTO;
731 static fe_transmit_mode_t DecodeTransmission( input_thread_t * p_input )
734 fe_transmit_mode_t fe_transmission = 0;
736 var_Get( p_input, "dvb-transmission", &val );
737 msg_Dbg( p_input, "using transmission=%d", val.i_int );
741 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
742 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
743 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
745 msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
746 fe_transmission = TRANSMISSION_MODE_AUTO;
749 return fe_transmission;
752 static fe_guard_interval_t DecodeGuardInterval( input_thread_t * p_input )
755 fe_guard_interval_t fe_guard = 0;
757 var_Get( p_input, "dvb-guard", &val );
758 msg_Dbg( p_input, "using guard=%d", val.i_int );
762 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
763 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
764 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
765 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
766 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
768 msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
769 fe_guard = GUARD_INTERVAL_AUTO;
775 static fe_hierarchy_t DecodeHierarchy( input_thread_t * p_input )
778 fe_hierarchy_t fe_hierarchy = 0;
780 var_Get( p_input, "dvb-hierarchy", &val );
781 msg_Dbg( p_input, "using hierarchy=%d", val.i_int );
785 case -1: fe_hierarchy = HIERARCHY_NONE; break;
786 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
787 case 1: fe_hierarchy = HIERARCHY_1; break;
788 case 2: fe_hierarchy = HIERARCHY_2; break;
789 case 4: fe_hierarchy = HIERARCHY_4; break;
791 msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
792 fe_hierarchy = HIERARCHY_AUTO;
798 static int FrontendSetOFDM( input_thread_t * p_input )
800 thread_dvb_data_t * p_dvb
801 = (thread_dvb_data_t *)p_input->p_access_data;
802 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
803 struct dvb_frontend_parameters fep;
807 /* Prepare the fep structure */
809 var_Get( p_input, "dvb-frequency", &val );
810 fep.frequency = val.i_int;
812 fep.inversion = DecodeInversion( p_input );
814 fep.u.ofdm.bandwidth = DecodeBandwidth( p_input );
815 var_Get( p_input, "dvb-code-rate-hp", &val );
816 fep.u.ofdm.code_rate_HP = DecodeFEC( p_input, val.i_int );
817 var_Get( p_input, "dvb-code-rate-lp", &val );
818 fep.u.ofdm.code_rate_LP = DecodeFEC( p_input, val.i_int );
819 fep.u.ofdm.constellation = DecodeModulation( p_input );
820 fep.u.ofdm.transmission_mode = DecodeTransmission( p_input );
821 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_input );
822 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_input );
824 /* Empty the event queue */
827 struct dvb_frontend_event event;
828 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
832 /* Now send it all to the frontend device */
833 if ( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
835 msg_Err( p_input, "DVB-T: setting frontend failed (%d) %s", ret,
840 return FrontendCheck( p_input );
843 /******************************************************************
844 * FrontendCheck: Check completion of the frontend control sequence
845 ******************************************************************/
846 static int FrontendCheck( input_thread_t * p_input )
848 thread_dvb_data_t * p_dvb
849 = (thread_dvb_data_t *)p_input->p_access_data;
850 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
853 while ( !p_input->b_die && !p_input->b_error )
856 if ( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
859 msg_Err( p_input, "reading frontend status failed (%d) %s",
860 i_ret, strerror(errno) );
864 if (status & FE_HAS_SIGNAL) /* found something above the noise level */
865 msg_Dbg(p_input, "check frontend ... has signal");
867 if (status & FE_HAS_CARRIER) /* found a DVB signal */
868 msg_Dbg(p_input, "check frontend ... has carrier");
870 if (status & FE_HAS_VITERBI) /* FEC is stable */
871 msg_Dbg(p_input, "check frontend ... has stable forward error correction");
873 if (status & FE_HAS_SYNC) /* found sync bytes */
874 msg_Dbg(p_input, "check frontend ... has sync");
876 if (status & FE_HAS_LOCK) /* everything's working... */
879 msg_Dbg(p_input, "check frontend ... has lock");
880 msg_Dbg(p_input, "tuning succeeded");
882 /* Read some statistics */
884 if ( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
885 msg_Dbg( p_input, "Bit error rate: %d", value );
888 if ( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
889 msg_Dbg( p_input, "Signal strength: %d", value );
892 if ( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
893 msg_Dbg( p_input, "SNR: %d", value );
898 if (status & FE_TIMEDOUT) /* no lock within the last ~2 seconds */
900 msg_Err(p_input, "tuning failed ... timed out");
904 if (status & FE_REINIT)
906 /* frontend was reinitialized, */
907 /* application is recommended to reset */
908 /* DiSEqC, tone and parameters */
909 msg_Err(p_input, "tuning failed ... resend frontend parameters");
923 /*****************************************************************************
924 * DMXSetFilter : controls the demux to add a filter
925 *****************************************************************************/
926 int E_(DMXSetFilter)( input_thread_t * p_input, int i_pid, int * pi_fd,
929 struct dmx_pes_filter_params s_filter_params;
931 unsigned int i_adapter, i_device;
935 var_Get( p_input, "dvb-adapter", &val );
936 i_adapter = val.i_int;
937 var_Get( p_input, "dvb-device", &val );
938 i_device = val.i_int;
940 if ( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
941 >= (int)sizeof(dmx) )
943 msg_Err( p_input, "snprintf() truncated string for DMX" );
944 dmx[sizeof(dmx) - 1] = '\0';
947 msg_Dbg( p_input, "Opening device %s", dmx );
948 if ( (*pi_fd = open(dmx, O_RDWR)) < 0 )
950 msg_Err( p_input, "DMXSetFilter: opening device failed (%s)",
955 /* We fill the DEMUX structure : */
956 s_filter_params.pid = i_pid;
957 s_filter_params.input = DMX_IN_FRONTEND;
958 s_filter_params.output = DMX_OUT_TS_TAP;
959 s_filter_params.flags = DMX_IMMEDIATE_START;
964 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
965 s_filter_params.pes_type = DMX_PES_VIDEO0;
968 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
969 s_filter_params.pes_type = DMX_PES_AUDIO0;
972 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
973 s_filter_params.pes_type = DMX_PES_TELETEXT0;
976 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
977 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
980 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
981 s_filter_params.pes_type = DMX_PES_PCR0;
985 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
986 s_filter_params.pes_type = DMX_PES_VIDEO1;
989 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
990 s_filter_params.pes_type = DMX_PES_AUDIO1;
993 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
994 s_filter_params.pes_type = DMX_PES_TELETEXT1;
997 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
998 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1001 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1002 s_filter_params.pes_type = DMX_PES_PCR1;
1006 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1007 s_filter_params.pes_type = DMX_PES_VIDEO2;
1010 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1011 s_filter_params.pes_type = DMX_PES_AUDIO2;
1014 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1015 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1018 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1019 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1022 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1023 s_filter_params.pes_type = DMX_PES_PCR2;
1027 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1028 s_filter_params.pes_type = DMX_PES_VIDEO3;
1031 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1032 s_filter_params.pes_type = DMX_PES_AUDIO3;
1035 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1036 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1039 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1040 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1043 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1044 s_filter_params.pes_type = DMX_PES_PCR3;
1046 /* Usually used by Nova cards */
1049 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1050 s_filter_params.pes_type = DMX_PES_OTHER;
1054 /* We then give the order to the device : */
1055 if ( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1057 msg_Err( p_input, "DMXSetFilter: failed with %d (%s)", i_ret,
1064 /*****************************************************************************
1065 * DMXUnsetFilter : removes a filter
1066 *****************************************************************************/
1067 int E_(DMXUnsetFilter)( input_thread_t * p_input, int i_fd )
1071 if ( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1073 msg_Err( p_input, "DMX_STOP failed for demux (%d) %s",
1074 i_ret, strerror(errno) );
1078 msg_Dbg( p_input, "DMXUnsetFilter: closing demux %d", i_fd);
1088 /*****************************************************************************
1090 *****************************************************************************/
1091 int E_(DVROpen)( input_thread_t * p_input )
1093 thread_dvb_data_t * p_dvb
1094 = (thread_dvb_data_t *)p_input->p_access_data;
1095 unsigned int i_adapter, i_device;
1099 var_Get( p_input, "dvb-adapter", &val );
1100 i_adapter = val.i_int;
1101 var_Get( p_input, "dvb-device", &val );
1102 i_device = val.i_int;
1104 if ( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1105 >= (int)sizeof(dvr) )
1107 msg_Err( p_input, "snprintf() truncated string for DVR" );
1108 dvr[sizeof(dvr) - 1] = '\0';
1111 msg_Dbg( p_input, "Opening device %s", dvr );
1112 if ( (p_dvb->i_handle = open(dvr, O_RDONLY)) < 0 )
1114 msg_Err( p_input, "DVROpen: opening device failed (%s)",
1122 /*****************************************************************************
1124 *****************************************************************************/
1125 void E_(DVRClose)( input_thread_t * p_input )
1127 thread_dvb_data_t * p_dvb
1128 = (thread_dvb_data_t *)p_input->p_access_data;
1130 close( p_dvb->i_handle );