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>
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>
46 #include <linux/errno.h>
51 #define DMX_BUFFER_SIZE (1024 * 1024)
59 struct dvb_frontend_info info;
62 /* Local prototypes */
63 static int FrontendInfo( access_t * );
64 static int FrontendSetQPSK( access_t * );
65 static int FrontendSetQAM( access_t * );
66 static int FrontendSetOFDM( access_t * );
67 static int FrontendCheck( access_t * );
69 /*****************************************************************************
70 * FrontendOpen : Determine frontend device information and capabilities
71 *****************************************************************************/
72 int E_(FrontendOpen)( access_t *p_access )
74 access_sys_t *p_sys = p_access->p_sys;
75 frontend_t * p_frontend;
76 unsigned int i_adapter, i_device;
80 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
81 i_device = var_GetInteger( p_access, "dvb-device" );
82 b_probe = var_GetBool( p_access, "dvb-probe" );
84 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
86 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
87 frontend[sizeof(frontend) - 1] = '\0';
90 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
92 msg_Dbg( p_access, "Opening device %s", frontend );
93 if( (p_frontend->i_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
95 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
103 char * psz_expected = NULL;
106 if( FrontendInfo( p_access ) < 0 )
108 close( p_frontend->i_handle );
113 switch( p_frontend->info.type )
125 psz_real = "unknown";
129 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
130 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
131 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
132 (p_frontend->info.type != FE_QPSK) )
134 psz_expected = "DVB-S";
136 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
137 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
138 (p_frontend->info.type != FE_QAM) )
140 psz_expected = "DVB-C";
142 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
143 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
144 (p_frontend->info.type != FE_OFDM) )
146 psz_expected = "DVB-T";
149 if( psz_expected != NULL )
151 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
152 psz_expected, psz_real );
153 close( p_frontend->i_handle );
158 else /* no frontend probing is done so use default border values. */
160 msg_Dbg( p_access, "using default values for frontend info" );
162 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
163 p_frontend->info.type = FE_QPSK;
164 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
165 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
166 p_frontend->info.type = FE_QPSK;
167 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
168 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
169 p_frontend->info.type = FE_QAM;
170 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
171 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
172 p_frontend->info.type = FE_OFDM;
178 /*****************************************************************************
179 * FrontendClose : Close the frontend
180 *****************************************************************************/
181 void E_(FrontendClose)( access_t *p_access )
183 access_sys_t *p_sys = p_access->p_sys;
185 if( p_sys->p_frontend )
187 close( p_sys->p_frontend->i_handle );
188 free( p_sys->p_frontend );
190 p_sys->p_frontend = NULL;
194 /*****************************************************************************
195 * FrontendSet : Tune !
196 *****************************************************************************/
197 int E_(FrontendSet)( access_t *p_access )
199 access_sys_t *p_sys = p_access->p_sys;
201 switch( p_sys->p_frontend->info.type )
203 /* DVB-S: satellite and budget cards (nova) */
205 if( FrontendSetQPSK( p_access ) < 0 )
207 msg_Err( p_access, "DVB-S: tuning failed" );
214 if( FrontendSetQAM( p_access ) < 0 )
216 msg_Err( p_access, "DVB-C: tuning failed" );
223 if( FrontendSetOFDM( p_access ) < 0 )
225 msg_Err( p_access, "DVB-T: tuning failed" );
231 msg_Err( p_access, "Could not determine frontend type on %s",
232 p_sys->p_frontend->info.name );
238 /*****************************************************************************
239 * FrontendInfo : Return information about given frontend
240 *****************************************************************************/
241 static int FrontendInfo( access_t *p_access )
243 access_sys_t *p_sys = p_access->p_sys;
244 frontend_t *p_frontend = p_sys->p_frontend;
247 /* Determine type of frontend */
248 if( (i_ret = ioctl( p_frontend->i_handle, FE_GET_INFO,
249 &p_frontend->info )) < 0 )
251 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
256 /* Print out frontend capabilities. */
257 msg_Dbg(p_access, "Frontend Info:" );
258 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
259 switch( p_frontend->info.type )
262 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
265 msg_Dbg( p_access, " type = QAM (DVB-C)" );
268 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
270 #if 0 /* DVB_API_VERSION == 3 */
272 msg_Dbg(p_access, " type = MEMORY" );
275 msg_Dbg(p_access, " type = NETWORK" );
279 msg_Err( p_access, " unknown frontend type (%d)",
280 p_frontend->info.type );
283 msg_Dbg(p_access, " frequency_min = %u (kHz)",
284 p_frontend->info.frequency_min);
285 msg_Dbg(p_access, " frequency_max = %u (kHz)",
286 p_frontend->info.frequency_max);
287 msg_Dbg(p_access, " frequency_stepsize = %u",
288 p_frontend->info.frequency_stepsize);
289 msg_Dbg(p_access, " frequency_tolerance = %u",
290 p_frontend->info.frequency_tolerance);
291 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
292 p_frontend->info.symbol_rate_min);
293 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
294 p_frontend->info.symbol_rate_max);
295 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
296 p_frontend->info.symbol_rate_tolerance);
297 msg_Dbg(p_access, " notifier_delay (ms) = %u",
298 p_frontend->info.notifier_delay );
300 msg_Dbg(p_access, "Frontend Info capability list:");
301 if( p_frontend->info.caps & FE_IS_STUPID)
302 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
303 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
304 msg_Dbg(p_access, " inversion auto");
305 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
306 msg_Dbg(p_access, " forward error correction 1/2");
307 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
308 msg_Dbg(p_access, " forward error correction 2/3");
309 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
310 msg_Dbg(p_access, " forward error correction 3/4");
311 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
312 msg_Dbg(p_access, " forward error correction 4/5");
313 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
314 msg_Dbg(p_access, " forward error correction 5/6");
315 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
316 msg_Dbg(p_access, " forward error correction 6/7");
317 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
318 msg_Dbg(p_access, " forward error correction 7/8");
319 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
320 msg_Dbg(p_access, " forward error correction 8/9");
321 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
322 msg_Dbg(p_access, " forward error correction auto");
323 if( p_frontend->info.caps & FE_CAN_QPSK)
324 msg_Dbg(p_access, " card can do QPSK");
325 if( p_frontend->info.caps & FE_CAN_QAM_16)
326 msg_Dbg(p_access, " card can do QAM 16");
327 if( p_frontend->info.caps & FE_CAN_QAM_32)
328 msg_Dbg(p_access, " card can do QAM 32");
329 if( p_frontend->info.caps & FE_CAN_QAM_64)
330 msg_Dbg(p_access, " card can do QAM 64");
331 if( p_frontend->info.caps & FE_CAN_QAM_128)
332 msg_Dbg(p_access, " card can do QAM 128");
333 if( p_frontend->info.caps & FE_CAN_QAM_256)
334 msg_Dbg(p_access, " card can do QAM 256");
335 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
336 msg_Dbg(p_access, " card can do QAM auto");
337 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
338 msg_Dbg(p_access, " transmission mode auto");
339 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
340 msg_Dbg(p_access, " bandwidth mode auto");
341 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
342 msg_Dbg(p_access, " guard interval mode auto");
343 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
344 msg_Dbg(p_access, " hierarchy mode auto");
345 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
346 msg_Dbg(p_access, " card can mute TS");
347 if( p_frontend->info.caps & FE_CAN_CLEAN_SETUP)
348 msg_Dbg(p_access, " clean setup");
349 msg_Dbg(p_access, "End of capability list");
354 /*****************************************************************************
355 * Decoding the DVB parameters (common)
356 *****************************************************************************/
357 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
360 fe_spectral_inversion_t fe_inversion = 0;
362 var_Get( p_access, "dvb-inversion", &val );
363 msg_Dbg( p_access, "using inversion=%d", val.i_int );
367 case 0: fe_inversion = INVERSION_OFF; break;
368 case 1: fe_inversion = INVERSION_ON; break;
369 case 2: fe_inversion = INVERSION_AUTO; break;
371 msg_Dbg( p_access, "dvb has inversion not set, using auto");
372 fe_inversion = INVERSION_AUTO;
378 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
380 fe_code_rate_t fe_fec = FEC_NONE;
382 msg_Dbg( p_access, "using fec=%d", i_val );
386 case 0: fe_fec = FEC_NONE; break;
387 case 1: fe_fec = FEC_1_2; break;
388 case 2: fe_fec = FEC_2_3; break;
389 case 3: fe_fec = FEC_3_4; break;
390 case 4: fe_fec = FEC_4_5; break;
391 case 5: fe_fec = FEC_5_6; break;
392 case 6: fe_fec = FEC_6_7; break;
393 case 7: fe_fec = FEC_7_8; break;
394 case 8: fe_fec = FEC_8_9; break;
395 case 9: fe_fec = FEC_AUTO; break;
399 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
405 static fe_modulation_t DecodeModulation( access_t *p_access )
408 fe_modulation_t fe_modulation = 0;
410 var_Get( p_access, "dvb-modulation", &val );
414 case -1: fe_modulation = QPSK; break;
415 case 0: fe_modulation = QAM_AUTO; break;
416 case 16: fe_modulation = QAM_16; break;
417 case 32: fe_modulation = QAM_32; break;
418 case 64: fe_modulation = QAM_64; break;
419 case 128: fe_modulation = QAM_128; break;
420 case 256: fe_modulation = QAM_256; break;
422 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
423 fe_modulation = QAM_AUTO;
426 return fe_modulation;
429 /*****************************************************************************
430 * FrontendSetQPSK : controls the FE device
431 *****************************************************************************/
432 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
435 fe_sec_voltage_t fe_voltage;
437 var_Get( p_access, "dvb-voltage", &val );
438 msg_Dbg( p_access, "using voltage=%d", val.i_int );
442 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
443 case 13: fe_voltage = SEC_VOLTAGE_13; break;
444 case 18: fe_voltage = SEC_VOLTAGE_18; break;
446 fe_voltage = SEC_VOLTAGE_OFF;
447 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
453 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
456 fe_sec_tone_mode_t fe_tone;
458 var_Get( p_access, "dvb-tone", &val );
459 msg_Dbg( p_access, "using tone=%d", val.i_int );
463 case 0: fe_tone = SEC_TONE_OFF; break;
464 case 1: fe_tone = SEC_TONE_ON; break;
466 fe_tone = SEC_TONE_OFF;
467 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
475 struct dvb_diseqc_master_cmd cmd;
479 static int DoDiseqc( access_t *p_access )
481 access_sys_t *p_sys = p_access->p_sys;
482 frontend_t * p_frontend = p_sys->p_frontend;
484 int i_frequency, i_lnb_slof;
485 fe_sec_voltage_t fe_voltage;
486 fe_sec_tone_mode_t fe_tone;
489 var_Get( p_access, "dvb-frequency", &val );
490 i_frequency = val.i_int;
491 var_Get( p_access, "dvb-lnb-slof", &val );
492 i_lnb_slof = val.i_int;
494 var_Get( p_access, "dvb-tone", &val );
495 if( val.i_int == -1 /* auto */ )
497 if( i_frequency >= i_lnb_slof )
501 var_Set( p_access, "dvb-tone", val );
504 fe_voltage = DecodeVoltage( p_access );
505 fe_tone = DecodeTone( p_access );
507 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
509 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
510 fe_voltage, i_err, strerror(errno) );
514 var_Get( p_access, "dvb-satno", &val );
517 /* digital satellite equipment control,
518 * specification is available from http://www.eutelsat.com/
520 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
521 SEC_TONE_OFF )) < 0 )
523 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
524 i_err, strerror(errno) );
530 if( val.i_int >= 1 && val.i_int <= 4 )
532 /* 1.x compatible equipment */
533 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
535 /* param: high nibble: reset bits, low nibble set bits,
536 * bits are: option, position, polarization, band
538 cmd.cmd.msg[3] = 0xf0 /* reset bits */
539 | (((val.i_int - 1) * 4) & 0xc)
540 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
541 | (fe_tone == SEC_TONE_ON ? 1 : 0);
543 if( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
546 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
547 i_err, strerror(errno) );
551 msleep(cmd.wait * 1000);
555 /* A or B simple diseqc */
556 if( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
557 val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
559 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
560 i_err, strerror(errno) );
568 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
570 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
571 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
580 static int FrontendSetQPSK( access_t *p_access )
582 access_sys_t *p_sys = p_access->p_sys;
583 frontend_t * p_frontend = p_sys->p_frontend;
584 struct dvb_frontend_parameters fep;
587 int i_frequency, i_lnb_slof;
589 /* Prepare the fep structure */
590 var_Get( p_access, "dvb-frequency", &val );
591 i_frequency = val.i_int;
592 var_Get( p_access, "dvb-lnb-slof", &val );
593 i_lnb_slof = val.i_int;
595 if( i_frequency >= i_lnb_slof )
596 var_Get( p_access, "dvb-lnb-lof2", &val );
598 var_Get( p_access, "dvb-lnb-lof1", &val );
599 fep.frequency = i_frequency - val.i_int;
601 fep.inversion = DecodeInversion( p_access );
603 var_Get( p_access, "dvb-srate", &val );
604 fep.u.qpsk.symbol_rate = val.i_int;
606 var_Get( p_access, "dvb-fec", &val );
607 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
609 if( DoDiseqc( p_access ) < 0 )
616 /* Empty the event queue */
619 struct dvb_frontend_event event;
620 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
624 /* Now send it all to the frontend device */
625 if( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
627 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
632 return FrontendCheck( p_access );
635 /*****************************************************************************
636 * FrontendSetQAM : controls the FE device
637 *****************************************************************************/
638 static int FrontendSetQAM( access_t *p_access )
640 access_sys_t *p_sys = p_access->p_sys;
641 frontend_t * p_frontend = p_sys->p_frontend;
642 struct dvb_frontend_parameters fep;
646 /* Prepare the fep structure */
648 var_Get( p_access, "dvb-frequency", &val );
649 fep.frequency = val.i_int;
651 fep.inversion = DecodeInversion( p_access );
653 var_Get( p_access, "dvb-srate", &val );
654 fep.u.qam.symbol_rate = val.i_int;
656 var_Get( p_access, "dvb-fec", &val );
657 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
659 fep.u.qam.modulation = DecodeModulation( p_access );
661 /* Empty the event queue */
664 struct dvb_frontend_event event;
665 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
669 /* Now send it all to the frontend device */
670 if( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
672 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
677 return FrontendCheck( p_access );
680 /*****************************************************************************
681 * FrontendSetOFDM : controls the FE device
682 *****************************************************************************/
683 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
686 fe_bandwidth_t fe_bandwidth = 0;
688 var_Get( p_access, "dvb-bandwidth", &val );
689 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
693 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
694 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
695 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
696 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
698 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
699 fe_bandwidth = BANDWIDTH_AUTO;
705 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
708 fe_transmit_mode_t fe_transmission = 0;
710 var_Get( p_access, "dvb-transmission", &val );
711 msg_Dbg( p_access, "using transmission=%d", val.i_int );
715 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
716 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
717 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
719 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
720 fe_transmission = TRANSMISSION_MODE_AUTO;
723 return fe_transmission;
726 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
729 fe_guard_interval_t fe_guard = 0;
731 var_Get( p_access, "dvb-guard", &val );
732 msg_Dbg( p_access, "using guard=%d", val.i_int );
736 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
737 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
738 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
739 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
740 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
742 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
743 fe_guard = GUARD_INTERVAL_AUTO;
749 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
752 fe_hierarchy_t fe_hierarchy = 0;
754 var_Get( p_access, "dvb-hierarchy", &val );
755 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
759 case -1: fe_hierarchy = HIERARCHY_NONE; break;
760 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
761 case 1: fe_hierarchy = HIERARCHY_1; break;
762 case 2: fe_hierarchy = HIERARCHY_2; break;
763 case 4: fe_hierarchy = HIERARCHY_4; break;
765 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
766 fe_hierarchy = HIERARCHY_AUTO;
772 static int FrontendSetOFDM( access_t * p_access )
774 access_sys_t *p_sys = p_access->p_sys;
775 frontend_t * p_frontend = p_sys->p_frontend;
776 struct dvb_frontend_parameters fep;
780 /* Prepare the fep structure */
782 var_Get( p_access, "dvb-frequency", &val );
783 fep.frequency = val.i_int;
785 fep.inversion = DecodeInversion( p_access );
787 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
788 var_Get( p_access, "dvb-code-rate-hp", &val );
789 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
790 var_Get( p_access, "dvb-code-rate-lp", &val );
791 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
792 fep.u.ofdm.constellation = DecodeModulation( p_access );
793 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
794 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
795 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
797 /* Empty the event queue */
800 struct dvb_frontend_event event;
801 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
805 /* Now send it all to the frontend device */
806 if( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
808 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
813 return FrontendCheck( p_access );
816 /******************************************************************
817 * FrontendCheck: Check completion of the frontend control sequence
818 ******************************************************************/
819 static int FrontendCheck( access_t * p_access )
821 access_sys_t *p_sys = p_access->p_sys;
822 frontend_t * p_frontend = p_sys->p_frontend;
825 while ( !p_access->b_die && !p_access->b_error )
828 if( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
831 msg_Err( p_access, "reading frontend status failed (%d) %s",
832 i_ret, strerror(errno) );
836 if(status & FE_HAS_SIGNAL) /* found something above the noise level */
837 msg_Dbg(p_access, "check frontend ... has signal");
839 if(status & FE_HAS_CARRIER) /* found a DVB signal */
840 msg_Dbg(p_access, "check frontend ... has carrier");
842 if(status & FE_HAS_VITERBI) /* FEC is stable */
843 msg_Dbg(p_access, "check frontend ... has stable forward error correction");
845 if(status & FE_HAS_SYNC) /* found sync bytes */
846 msg_Dbg(p_access, "check frontend ... has sync");
848 if(status & FE_HAS_LOCK) /* everything's working... */
851 msg_Dbg(p_access, "check frontend ... has lock");
852 msg_Dbg(p_access, "tuning succeeded");
854 /* Read some statistics */
856 if( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
857 msg_Dbg( p_access, "Bit error rate: %d", value );
860 if( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
861 msg_Dbg( p_access, "Signal strength: %d", value );
864 if( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
865 msg_Dbg( p_access, "SNR: %d", value );
870 if(status & FE_TIMEDOUT) /* no lock within the last ~2 seconds */
872 msg_Err(p_access, "tuning failed ... timed out");
876 if(status & FE_REINIT)
878 /* frontend was reinitialized, */
879 /* application is recommended to reset */
880 /* DiSEqC, tone and parameters */
881 msg_Err(p_access, "tuning failed ... resend frontend parameters");
895 /*****************************************************************************
896 * DMXSetFilter : controls the demux to add a filter
897 *****************************************************************************/
898 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
900 struct dmx_pes_filter_params s_filter_params;
902 unsigned int i_adapter, i_device;
906 var_Get( p_access, "dvb-adapter", &val );
907 i_adapter = val.i_int;
908 var_Get( p_access, "dvb-device", &val );
909 i_device = val.i_int;
911 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
912 >= (int)sizeof(dmx) )
914 msg_Err( p_access, "snprintf() truncated string for DMX" );
915 dmx[sizeof(dmx) - 1] = '\0';
918 msg_Dbg( p_access, "Opening device %s", dmx );
919 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
921 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
926 /* We fill the DEMUX structure : */
927 s_filter_params.pid = i_pid;
928 s_filter_params.input = DMX_IN_FRONTEND;
929 s_filter_params.output = DMX_OUT_TS_TAP;
930 s_filter_params.flags = DMX_IMMEDIATE_START;
935 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
936 s_filter_params.pes_type = DMX_PES_VIDEO0;
939 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
940 s_filter_params.pes_type = DMX_PES_AUDIO0;
943 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
944 s_filter_params.pes_type = DMX_PES_TELETEXT0;
947 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
948 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
951 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
952 s_filter_params.pes_type = DMX_PES_PCR0;
956 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
957 s_filter_params.pes_type = DMX_PES_VIDEO1;
960 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
961 s_filter_params.pes_type = DMX_PES_AUDIO1;
964 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
965 s_filter_params.pes_type = DMX_PES_TELETEXT1;
968 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
969 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
972 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
973 s_filter_params.pes_type = DMX_PES_PCR1;
977 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
978 s_filter_params.pes_type = DMX_PES_VIDEO2;
981 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
982 s_filter_params.pes_type = DMX_PES_AUDIO2;
985 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
986 s_filter_params.pes_type = DMX_PES_TELETEXT2;
989 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
990 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
993 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
994 s_filter_params.pes_type = DMX_PES_PCR2;
998 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
999 s_filter_params.pes_type = DMX_PES_VIDEO3;
1002 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1003 s_filter_params.pes_type = DMX_PES_AUDIO3;
1006 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1007 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1010 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1011 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1014 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1015 s_filter_params.pes_type = DMX_PES_PCR3;
1017 /* Usually used by Nova cards */
1020 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1021 s_filter_params.pes_type = DMX_PES_OTHER;
1025 /* We then give the order to the device : */
1026 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1028 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1030 return VLC_EGENERIC;
1035 /*****************************************************************************
1036 * DMXUnsetFilter : removes a filter
1037 *****************************************************************************/
1038 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1042 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1044 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1045 i_ret, strerror(errno) );
1049 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1059 /*****************************************************************************
1061 *****************************************************************************/
1062 int E_(DVROpen)( access_t * p_access )
1064 access_sys_t *p_sys = p_access->p_sys;
1065 unsigned int i_adapter, i_device;
1069 var_Get( p_access, "dvb-adapter", &val );
1070 i_adapter = val.i_int;
1071 var_Get( p_access, "dvb-device", &val );
1072 i_device = val.i_int;
1074 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1075 >= (int)sizeof(dvr) )
1077 msg_Err( p_access, "snprintf() truncated string for DVR" );
1078 dvr[sizeof(dvr) - 1] = '\0';
1081 msg_Dbg( p_access, "Opening device %s", dvr );
1082 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1084 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1086 return VLC_EGENERIC;
1089 if ( ioctl( p_sys->i_handle, DMX_SET_BUFFER_SIZE, DMX_BUFFER_SIZE ) < 0 )
1091 msg_Warn( p_access, "couldn't set DMX_BUFFER_SIZE (%s)",
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 void E_(DVRClose)( access_t * p_access )
1103 access_sys_t *p_sys = p_access->p_sys;
1105 close( p_sys->i_handle );
1112 * This uses the external cam_set program from libdvb-0.5.4
1115 /*****************************************************************************
1117 *****************************************************************************/
1118 int E_(CAMOpen)( access_t * p_access )
1120 access_sys_t *p_sys = p_access->p_sys;
1122 p_sys->i_cam_handle = net_OpenTCP( p_access, "localhost", 4711 );
1123 if ( p_sys->i_cam_handle < 0 )
1125 return -VLC_EGENERIC;
1131 /*****************************************************************************
1133 *****************************************************************************/
1134 int E_(CAMSet)( access_t * p_access, uint16_t i_program, uint16_t i_vpid,
1135 uint16_t i_apid1, uint16_t i_apid2, uint16_t i_apid3,
1136 uint16_t i_cad_length, uint8_t *p_cad )
1138 access_sys_t *p_sys = p_access->p_sys;
1141 memcpy( p_str, &i_program, 2 );
1142 memcpy( p_str + 2, &i_vpid, 2 );
1143 memcpy( p_str + 4, &i_apid1, 2 );
1144 memcpy( p_str + 6, &i_apid2, 2 );
1145 memcpy( p_str + 8, &i_apid3, 2 );
1146 memcpy( p_str + 10, &i_cad_length, 2 );
1148 if ( net_Write( p_access, p_sys->i_cam_handle, p_str, 12 ) != 12 )
1150 msg_Err( p_access, "write 1 failed (%s)", strerror(errno) );
1151 return -VLC_EGENERIC;
1156 if ( net_Write( p_access, p_sys->i_cam_handle, p_cad, i_cad_length )
1159 msg_Err( p_access, "write 2 failed (%s) %d", strerror(errno),
1161 return -VLC_EGENERIC;
1168 /*****************************************************************************
1170 *****************************************************************************/
1171 void E_(CAMClose)( access_t * p_access )
1173 access_sys_t *p_sys = p_access->p_sys;
1175 if ( p_sys->i_cam_handle )
1177 close( p_sys->i_cam_handle );