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 feq=%d", i_val );
408 case 1: fe_fec = FEC_1_2; break;
409 case 2: fe_fec = FEC_2_3; break;
410 case 3: fe_fec = FEC_3_4; break;
411 case 4: fe_fec = FEC_4_5; break;
412 case 5: fe_fec = FEC_5_6; break;
413 case 6: fe_fec = FEC_6_7; break;
414 case 7: fe_fec = FEC_7_8; break;
415 case 8: fe_fec = FEC_8_9; break;
416 case 9: fe_fec = FEC_AUTO; break;
420 msg_Err( p_input, "argument has invalid FEC (%d)", i_val);
426 static fe_modulation_t DecodeModulation( input_thread_t * p_input )
429 fe_modulation_t fe_modulation = 0;
431 var_Get( p_input, "dvb-modulation", &val );
435 case -1: fe_modulation = QPSK; break;
436 case 0: fe_modulation = QAM_AUTO; break;
437 case 16: fe_modulation = QAM_16; break;
438 case 32: fe_modulation = QAM_32; break;
439 case 64: fe_modulation = QAM_64; break;
440 case 128: fe_modulation = QAM_128; break;
441 case 256: fe_modulation = QAM_256; break;
443 msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
444 fe_modulation = QAM_AUTO;
447 return fe_modulation;
450 /*****************************************************************************
451 * FrontendSetQPSK : controls the FE device
452 *****************************************************************************/
453 static fe_sec_voltage_t DecodeVoltage( input_thread_t * p_input )
456 fe_sec_voltage_t fe_voltage;
458 var_Get( p_input, "dvb-voltage", &val );
459 msg_Dbg( p_input, "using voltage=%d", val.i_int );
463 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
464 case 13: fe_voltage = SEC_VOLTAGE_13; break;
465 case 18: fe_voltage = SEC_VOLTAGE_18; break;
467 fe_voltage = SEC_VOLTAGE_OFF;
468 msg_Err( p_input, "argument has invalid voltage (%d)", val.i_int);
474 static fe_sec_tone_mode_t DecodeTone( input_thread_t * p_input )
477 fe_sec_tone_mode_t fe_tone;
479 var_Get( p_input, "dvb-tone", &val );
480 msg_Dbg( p_input, "using tone=%d", val.i_int );
484 case 0: fe_tone = SEC_TONE_OFF; break;
485 case 1: fe_tone = SEC_TONE_ON; break;
487 fe_tone = SEC_TONE_OFF;
488 msg_Err( p_input, "argument has invalid tone mode (%d)", val.i_int);
496 struct dvb_diseqc_master_cmd cmd;
500 static int DoDiseqc( input_thread_t * p_input )
502 thread_dvb_data_t * p_dvb
503 = (thread_dvb_data_t *)p_input->p_access_data;
504 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
506 int i_frequency, i_lnb_slof;
507 fe_sec_voltage_t fe_voltage;
508 fe_sec_tone_mode_t fe_tone;
511 var_Get( p_input, "dvb-frequency", &val );
512 i_frequency = val.i_int;
513 var_Get( p_input, "dvb-lnb-slof", &val );
514 i_lnb_slof = val.i_int;
516 var_Get( p_input, "dvb-tone", &val );
517 if ( val.i_int == -1 /* auto */ )
519 if ( i_frequency >= i_lnb_slof )
523 var_Set( p_input, "dvb-tone", val );
526 fe_voltage = DecodeVoltage( p_input );
527 fe_tone = DecodeTone( p_input );
529 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
531 msg_Err( p_input, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
532 fe_voltage, i_err, strerror(errno) );
536 var_Get( p_input, "dvb-satno", &val );
537 if ( val.i_int != 0 )
539 /* digital satellite equipment control,
540 * specification is available from http://www.eutelsat.com/
542 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
543 SEC_TONE_OFF )) < 0 )
545 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
546 i_err, strerror(errno) );
552 if ( val.i_int >= 1 && val.i_int <= 4 )
554 /* 1.x compatible equipment */
555 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
557 /* param: high nibble: reset bits, low nibble set bits,
558 * bits are: option, position, polarization, band
560 cmd.cmd.msg[3] = 0xf0 /* reset bits */
561 | (((val.i_int - 1) * 4) & 0xc)
562 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
563 | (fe_tone == SEC_TONE_ON ? 1 : 0);
565 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
568 msg_Err( p_input, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
569 i_err, strerror(errno) );
573 msleep(cmd.wait * 1000);
577 /* A or B simple diseqc */
578 if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
579 val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
581 msg_Err( p_input, "ioctl FE_SEND_BURST failed (%d) %s",
582 i_err, strerror(errno) );
590 if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
592 msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
593 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
602 static int FrontendSetQPSK( input_thread_t * p_input )
604 thread_dvb_data_t * p_dvb
605 = (thread_dvb_data_t *)p_input->p_access_data;
606 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
607 struct dvb_frontend_parameters fep;
610 int i_frequency, i_lnb_slof;
612 /* Prepare the fep structure */
614 var_Get( p_input, "dvb-frequency", &val );
615 i_frequency = val.i_int;
616 var_Get( p_input, "dvb-lnb-slof", &val );
617 i_lnb_slof = val.i_int;
619 if ( i_frequency >= i_lnb_slof )
620 var_Get( p_input, "dvb-lnb-lof2", &val );
622 var_Get( p_input, "dvb-lnb-lof1", &val );
623 fep.frequency = i_frequency - val.i_int;
625 fep.inversion = DecodeInversion( p_input );
627 var_Get( p_input, "dvb-srate", &val );
628 fep.u.qpsk.symbol_rate = val.i_int;
630 var_Get( p_input, "dvb-fec", &val );
631 fep.u.qpsk.fec_inner = DecodeFEC( p_input, val.i_int );
633 if ( DoDiseqc( p_input ) < 0 )
640 /* Empty the event queue */
643 struct dvb_frontend_event event;
644 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
648 /* Now send it all to the frontend device */
649 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
651 msg_Err( p_input, "DVB-S: setting frontend failed (%d) %s", i_ret,
656 return FrontendCheck( p_input );
659 /*****************************************************************************
660 * FrontendSetQAM : controls the FE device
661 *****************************************************************************/
662 static int FrontendSetQAM( input_thread_t * p_input )
664 thread_dvb_data_t * p_dvb
665 = (thread_dvb_data_t *)p_input->p_access_data;
666 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
667 struct dvb_frontend_parameters fep;
671 /* Prepare the fep structure */
673 var_Get( p_input, "dvb-frequency", &val );
674 fep.frequency = val.i_int;
676 fep.inversion = DecodeInversion( p_input );
678 var_Get( p_input, "dvb-srate", &val );
679 fep.u.qam.symbol_rate = val.i_int;
681 var_Get( p_input, "dvb-fec", &val );
682 fep.u.qam.fec_inner = DecodeFEC( p_input, val.i_int );
684 fep.u.qam.modulation = DecodeModulation( p_input );
686 /* Empty the event queue */
689 struct dvb_frontend_event event;
690 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
694 /* Now send it all to the frontend device */
695 if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
697 msg_Err( p_input, "DVB-C: setting frontend failed (%d) %s", i_ret,
702 return FrontendCheck( p_input );
705 /*****************************************************************************
706 * FrontendSetOFDM : controls the FE device
707 *****************************************************************************/
708 static fe_bandwidth_t DecodeBandwidth( input_thread_t * p_input )
711 fe_bandwidth_t fe_bandwidth = 0;
713 var_Get( p_input, "dvb-bandwidth", &val );
714 msg_Dbg( p_input, "using bandwidth=%d", val.i_int );
718 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
719 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
720 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
721 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
723 msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto" );
724 fe_bandwidth = BANDWIDTH_AUTO;
730 static fe_transmit_mode_t DecodeTransmission( input_thread_t * p_input )
733 fe_transmit_mode_t fe_transmission = 0;
735 var_Get( p_input, "dvb-transmission", &val );
736 msg_Dbg( p_input, "using transmission=%d", val.i_int );
740 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
741 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
742 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
744 msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
745 fe_transmission = TRANSMISSION_MODE_AUTO;
748 return fe_transmission;
751 static fe_guard_interval_t DecodeGuardInterval( input_thread_t * p_input )
754 fe_guard_interval_t fe_guard = 0;
756 var_Get( p_input, "dvb-guard", &val );
757 msg_Dbg( p_input, "using guard=%d", val.i_int );
761 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
762 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
763 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
764 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
765 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
767 msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
768 fe_guard = GUARD_INTERVAL_AUTO;
774 static fe_hierarchy_t DecodeHierarchy( input_thread_t * p_input )
777 fe_hierarchy_t fe_hierarchy = 0;
779 var_Get( p_input, "dvb-hierarchy", &val );
780 msg_Dbg( p_input, "using hierarchy=%d", val.i_int );
784 case -1: fe_hierarchy = HIERARCHY_NONE; break;
785 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
786 case 1: fe_hierarchy = HIERARCHY_1; break;
787 case 2: fe_hierarchy = HIERARCHY_2; break;
788 case 4: fe_hierarchy = HIERARCHY_4; break;
790 msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
791 fe_hierarchy = HIERARCHY_AUTO;
797 static int FrontendSetOFDM( input_thread_t * p_input )
799 thread_dvb_data_t * p_dvb
800 = (thread_dvb_data_t *)p_input->p_access_data;
801 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
802 struct dvb_frontend_parameters fep;
806 /* Prepare the fep structure */
808 var_Get( p_input, "dvb-frequency", &val );
809 fep.frequency = val.i_int;
811 fep.inversion = DecodeInversion( p_input );
813 fep.u.ofdm.bandwidth = DecodeBandwidth( p_input );
814 var_Get( p_input, "dvb-code-rate-HP", &val );
815 fep.u.ofdm.code_rate_HP = DecodeFEC( p_input, val.i_int );
816 var_Get( p_input, "dvb-code-rate-LP", &val );
817 fep.u.ofdm.code_rate_LP = DecodeFEC( p_input, val.i_int );
818 fep.u.ofdm.constellation = DecodeModulation( p_input );
819 fep.u.ofdm.transmission_mode = DecodeTransmission( p_input );
820 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_input );
821 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_input );
823 /* Empty the event queue */
826 struct dvb_frontend_event event;
827 if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
831 /* Now send it all to the frontend device */
832 if ( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
834 msg_Err( p_input, "DVB-T: setting frontend failed (%d) %s", ret,
839 return FrontendCheck( p_input );
842 /******************************************************************
843 * FrontendCheck: Check completion of the frontend control sequence
844 ******************************************************************/
845 static int FrontendCheck( input_thread_t * p_input )
847 thread_dvb_data_t * p_dvb
848 = (thread_dvb_data_t *)p_input->p_access_data;
849 frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
852 while ( !p_input->b_die && !p_input->b_error )
855 if ( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
858 msg_Err( p_input, "reading frontend status failed (%d) %s",
859 i_ret, strerror(errno) );
863 if (status & FE_HAS_SIGNAL) /* found something above the noise level */
864 msg_Dbg(p_input, "check frontend ... has signal");
866 if (status & FE_HAS_CARRIER) /* found a DVB signal */
867 msg_Dbg(p_input, "check frontend ... has carrier");
869 if (status & FE_HAS_VITERBI) /* FEC is stable */
870 msg_Dbg(p_input, "check frontend ... has stable forward error correction");
872 if (status & FE_HAS_SYNC) /* found sync bytes */
873 msg_Dbg(p_input, "check frontend ... has sync");
875 if (status & FE_HAS_LOCK) /* everything's working... */
878 msg_Dbg(p_input, "check frontend ... has lock");
879 msg_Dbg(p_input, "tuning succeeded");
881 /* Read some statistics */
883 if ( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
884 msg_Dbg( p_input, "Bit error rate: %d", value );
887 if ( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
888 msg_Dbg( p_input, "Signal strength: %d", value );
891 if ( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
892 msg_Dbg( p_input, "SNR: %d", value );
897 if (status & FE_TIMEDOUT) /* no lock within the last ~2 seconds */
899 msg_Err(p_input, "tuning failed ... timed out");
903 if (status & FE_REINIT)
905 /* frontend was reinitialized, */
906 /* application is recommended to reset */
907 /* DiSEqC, tone and parameters */
908 msg_Err(p_input, "tuning failed ... resend frontend parameters");
922 /*****************************************************************************
923 * DMXSetFilter : controls the demux to add a filter
924 *****************************************************************************/
925 int E_(DMXSetFilter)( input_thread_t * p_input, int i_pid, int * pi_fd,
928 struct dmx_pes_filter_params s_filter_params;
930 unsigned int i_adapter, i_device;
934 var_Get( p_input, "dvb-adapter", &val );
935 i_adapter = val.i_int;
936 var_Get( p_input, "dvb-device", &val );
937 i_device = val.i_int;
939 if ( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
940 >= (int)sizeof(dmx) )
942 msg_Err( p_input, "snprintf() truncated string for DMX" );
943 dmx[sizeof(dmx) - 1] = '\0';
946 msg_Dbg( p_input, "Opening device %s", dmx );
947 if ( (*pi_fd = open(dmx, O_RDWR)) < 0 )
949 msg_Err( p_input, "DMXSetFilter: opening device failed (%s)",
954 /* We fill the DEMUX structure : */
955 s_filter_params.pid = i_pid;
956 s_filter_params.input = DMX_IN_FRONTEND;
957 s_filter_params.output = DMX_OUT_TS_TAP;
958 s_filter_params.flags = DMX_IMMEDIATE_START;
963 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
964 s_filter_params.pes_type = DMX_PES_VIDEO0;
967 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
968 s_filter_params.pes_type = DMX_PES_AUDIO0;
971 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
972 s_filter_params.pes_type = DMX_PES_TELETEXT0;
975 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
976 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
979 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
980 s_filter_params.pes_type = DMX_PES_PCR0;
984 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
985 s_filter_params.pes_type = DMX_PES_VIDEO1;
988 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
989 s_filter_params.pes_type = DMX_PES_AUDIO1;
992 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
993 s_filter_params.pes_type = DMX_PES_TELETEXT1;
996 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
997 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1000 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1001 s_filter_params.pes_type = DMX_PES_PCR1;
1005 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1006 s_filter_params.pes_type = DMX_PES_VIDEO2;
1009 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1010 s_filter_params.pes_type = DMX_PES_AUDIO2;
1013 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1014 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1017 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1018 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1021 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1022 s_filter_params.pes_type = DMX_PES_PCR2;
1026 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1027 s_filter_params.pes_type = DMX_PES_VIDEO3;
1030 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1031 s_filter_params.pes_type = DMX_PES_AUDIO3;
1034 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1035 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1038 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1039 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1042 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1043 s_filter_params.pes_type = DMX_PES_PCR3;
1045 /* Usually used by Nova cards */
1048 msg_Dbg(p_input, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1049 s_filter_params.pes_type = DMX_PES_OTHER;
1053 /* We then give the order to the device : */
1054 if ( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1056 msg_Err( p_input, "DMXSetFilter: failed with %d (%s)", i_ret,
1063 /*****************************************************************************
1064 * DMXUnsetFilter : removes a filter
1065 *****************************************************************************/
1066 int E_(DMXUnsetFilter)( input_thread_t * p_input, int i_fd )
1070 if ( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1072 msg_Err( p_input, "DMX_STOP failed for demux (%d) %s",
1073 i_ret, strerror(errno) );
1077 msg_Dbg( p_input, "DMXUnsetFilter: closing demux %d", i_fd);
1087 /*****************************************************************************
1089 *****************************************************************************/
1090 int E_(DVROpen)( input_thread_t * p_input )
1092 thread_dvb_data_t * p_dvb
1093 = (thread_dvb_data_t *)p_input->p_access_data;
1094 unsigned int i_adapter, i_device;
1098 var_Get( p_input, "dvb-adapter", &val );
1099 i_adapter = val.i_int;
1100 var_Get( p_input, "dvb-device", &val );
1101 i_device = val.i_int;
1103 if ( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1104 >= (int)sizeof(dvr) )
1106 msg_Err( p_input, "snprintf() truncated string for DVR" );
1107 dvr[sizeof(dvr) - 1] = '\0';
1110 msg_Dbg( p_input, "Opening device %s", dvr );
1111 if ( (p_dvb->i_handle = open(dvr, O_RDONLY)) < 0 )
1113 msg_Err( p_input, "DVROpen: opening device failed (%s)",
1121 /*****************************************************************************
1123 *****************************************************************************/
1124 void E_(DVRClose)( input_thread_t * p_input )
1126 thread_dvb_data_t * p_dvb
1127 = (thread_dvb_data_t *)p_input->p_access_data;
1129 close( p_dvb->i_handle );