1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
6 * Authors: Damien Lucas <nitrox@via.ecp.fr>
7 * Johan Bilien <jobi@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman@wxs.nl>
9 * Christopher Ross <chris@tebibyte.org>
10 * Christophe Massiot <massiot@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
28 #include <vlc/input.h>
30 #include <sys/ioctl.h>
33 #include <sys/types.h>
41 /* DVB Card Drivers */
42 #include <linux/dvb/version.h>
43 #include <linux/dvb/dmx.h>
44 #include <linux/dvb/frontend.h>
45 #include <linux/dvb/ca.h>
49 #define DMX_BUFFER_SIZE (1024 * 1024)
57 struct dvb_frontend_info info;
60 /* Local prototypes */
61 static int FrontendInfo( access_t * );
62 static int FrontendSetQPSK( access_t * );
63 static int FrontendSetQAM( access_t * );
64 static int FrontendSetOFDM( access_t * );
65 static int FrontendCheck( access_t * );
67 /*****************************************************************************
68 * FrontendOpen : Determine frontend device information and capabilities
69 *****************************************************************************/
70 int E_(FrontendOpen)( access_t *p_access )
72 access_sys_t *p_sys = p_access->p_sys;
73 frontend_t * p_frontend;
74 unsigned int i_adapter, i_device;
78 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
79 i_device = var_GetInteger( p_access, "dvb-device" );
80 b_probe = var_GetBool( p_access, "dvb-probe" );
82 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
84 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
85 frontend[sizeof(frontend) - 1] = '\0';
88 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
90 msg_Dbg( p_access, "Opening device %s", frontend );
91 if( (p_frontend->i_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
93 msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
101 char * psz_expected = NULL;
104 if( FrontendInfo( p_access ) < 0 )
106 close( p_frontend->i_handle );
111 switch( p_frontend->info.type )
123 psz_real = "unknown";
127 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
128 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
129 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
130 (p_frontend->info.type != FE_QPSK) )
132 psz_expected = "DVB-S";
134 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
135 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
136 (p_frontend->info.type != FE_QAM) )
138 psz_expected = "DVB-C";
140 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
141 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
142 (p_frontend->info.type != FE_OFDM) )
144 psz_expected = "DVB-T";
147 if( psz_expected != NULL )
149 msg_Err( p_access, "the user asked for %s, and the tuner is %s",
150 psz_expected, psz_real );
151 close( p_frontend->i_handle );
156 else /* no frontend probing is done so use default border values. */
158 msg_Dbg( p_access, "using default values for frontend info" );
160 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
161 p_frontend->info.type = FE_QPSK;
162 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
163 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
164 p_frontend->info.type = FE_QPSK;
165 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
166 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
167 p_frontend->info.type = FE_QAM;
168 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
169 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
170 p_frontend->info.type = FE_OFDM;
176 /*****************************************************************************
177 * FrontendClose : Close the frontend
178 *****************************************************************************/
179 void E_(FrontendClose)( access_t *p_access )
181 access_sys_t *p_sys = p_access->p_sys;
183 if( p_sys->p_frontend )
185 close( p_sys->p_frontend->i_handle );
186 free( p_sys->p_frontend );
188 p_sys->p_frontend = NULL;
192 /*****************************************************************************
193 * FrontendSet : Tune !
194 *****************************************************************************/
195 int E_(FrontendSet)( access_t *p_access )
197 access_sys_t *p_sys = p_access->p_sys;
199 switch( p_sys->p_frontend->info.type )
201 /* DVB-S: satellite and budget cards (nova) */
203 if( FrontendSetQPSK( p_access ) < 0 )
205 msg_Err( p_access, "DVB-S: tuning failed" );
212 if( FrontendSetQAM( p_access ) < 0 )
214 msg_Err( p_access, "DVB-C: tuning failed" );
221 if( FrontendSetOFDM( p_access ) < 0 )
223 msg_Err( p_access, "DVB-T: tuning failed" );
229 msg_Err( p_access, "Could not determine frontend type on %s",
230 p_sys->p_frontend->info.name );
236 /*****************************************************************************
237 * FrontendInfo : Return information about given frontend
238 *****************************************************************************/
239 static int FrontendInfo( access_t *p_access )
241 access_sys_t *p_sys = p_access->p_sys;
242 frontend_t *p_frontend = p_sys->p_frontend;
245 /* Determine type of frontend */
246 if( (i_ret = ioctl( p_frontend->i_handle, FE_GET_INFO,
247 &p_frontend->info )) < 0 )
249 msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
254 /* Print out frontend capabilities. */
255 msg_Dbg(p_access, "Frontend Info:" );
256 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
257 switch( p_frontend->info.type )
260 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
263 msg_Dbg( p_access, " type = QAM (DVB-C)" );
266 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
268 #if 0 /* DVB_API_VERSION == 3 */
270 msg_Dbg(p_access, " type = MEMORY" );
273 msg_Dbg(p_access, " type = NETWORK" );
277 msg_Err( p_access, " unknown frontend type (%d)",
278 p_frontend->info.type );
281 msg_Dbg(p_access, " frequency_min = %u (kHz)",
282 p_frontend->info.frequency_min);
283 msg_Dbg(p_access, " frequency_max = %u (kHz)",
284 p_frontend->info.frequency_max);
285 msg_Dbg(p_access, " frequency_stepsize = %u",
286 p_frontend->info.frequency_stepsize);
287 msg_Dbg(p_access, " frequency_tolerance = %u",
288 p_frontend->info.frequency_tolerance);
289 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
290 p_frontend->info.symbol_rate_min);
291 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
292 p_frontend->info.symbol_rate_max);
293 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
294 p_frontend->info.symbol_rate_tolerance);
295 msg_Dbg(p_access, " notifier_delay (ms) = %u",
296 p_frontend->info.notifier_delay );
298 msg_Dbg(p_access, "Frontend Info capability list:");
299 if( p_frontend->info.caps & FE_IS_STUPID)
300 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
301 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
302 msg_Dbg(p_access, " inversion auto");
303 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
304 msg_Dbg(p_access, " forward error correction 1/2");
305 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
306 msg_Dbg(p_access, " forward error correction 2/3");
307 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
308 msg_Dbg(p_access, " forward error correction 3/4");
309 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
310 msg_Dbg(p_access, " forward error correction 4/5");
311 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
312 msg_Dbg(p_access, " forward error correction 5/6");
313 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
314 msg_Dbg(p_access, " forward error correction 6/7");
315 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
316 msg_Dbg(p_access, " forward error correction 7/8");
317 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
318 msg_Dbg(p_access, " forward error correction 8/9");
319 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
320 msg_Dbg(p_access, " forward error correction auto");
321 if( p_frontend->info.caps & FE_CAN_QPSK)
322 msg_Dbg(p_access, " card can do QPSK");
323 if( p_frontend->info.caps & FE_CAN_QAM_16)
324 msg_Dbg(p_access, " card can do QAM 16");
325 if( p_frontend->info.caps & FE_CAN_QAM_32)
326 msg_Dbg(p_access, " card can do QAM 32");
327 if( p_frontend->info.caps & FE_CAN_QAM_64)
328 msg_Dbg(p_access, " card can do QAM 64");
329 if( p_frontend->info.caps & FE_CAN_QAM_128)
330 msg_Dbg(p_access, " card can do QAM 128");
331 if( p_frontend->info.caps & FE_CAN_QAM_256)
332 msg_Dbg(p_access, " card can do QAM 256");
333 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
334 msg_Dbg(p_access, " card can do QAM auto");
335 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
336 msg_Dbg(p_access, " transmission mode auto");
337 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
338 msg_Dbg(p_access, " bandwidth mode auto");
339 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
340 msg_Dbg(p_access, " guard interval mode auto");
341 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
342 msg_Dbg(p_access, " hierarchy mode auto");
343 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
344 msg_Dbg(p_access, " card can mute TS");
345 if( p_frontend->info.caps & FE_CAN_CLEAN_SETUP)
346 msg_Dbg(p_access, " clean setup");
347 msg_Dbg(p_access, "End of capability list");
352 /*****************************************************************************
353 * Decoding the DVB parameters (common)
354 *****************************************************************************/
355 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
358 fe_spectral_inversion_t fe_inversion = 0;
360 var_Get( p_access, "dvb-inversion", &val );
361 msg_Dbg( p_access, "using inversion=%d", val.i_int );
365 case 0: fe_inversion = INVERSION_OFF; break;
366 case 1: fe_inversion = INVERSION_ON; break;
367 case 2: fe_inversion = INVERSION_AUTO; break;
369 msg_Dbg( p_access, "dvb has inversion not set, using auto");
370 fe_inversion = INVERSION_AUTO;
376 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
378 fe_code_rate_t fe_fec = FEC_NONE;
380 msg_Dbg( p_access, "using fec=%d", i_val );
384 case 0: fe_fec = FEC_NONE; break;
385 case 1: fe_fec = FEC_1_2; break;
386 case 2: fe_fec = FEC_2_3; break;
387 case 3: fe_fec = FEC_3_4; break;
388 case 4: fe_fec = FEC_4_5; break;
389 case 5: fe_fec = FEC_5_6; break;
390 case 6: fe_fec = FEC_6_7; break;
391 case 7: fe_fec = FEC_7_8; break;
392 case 8: fe_fec = FEC_8_9; break;
393 case 9: fe_fec = FEC_AUTO; break;
397 msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
403 static fe_modulation_t DecodeModulation( access_t *p_access )
406 fe_modulation_t fe_modulation = 0;
408 var_Get( p_access, "dvb-modulation", &val );
412 case -1: fe_modulation = QPSK; break;
413 case 0: fe_modulation = QAM_AUTO; break;
414 case 16: fe_modulation = QAM_16; break;
415 case 32: fe_modulation = QAM_32; break;
416 case 64: fe_modulation = QAM_64; break;
417 case 128: fe_modulation = QAM_128; break;
418 case 256: fe_modulation = QAM_256; break;
420 msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
421 fe_modulation = QAM_AUTO;
424 return fe_modulation;
427 /*****************************************************************************
428 * FrontendSetQPSK : controls the FE device
429 *****************************************************************************/
430 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
433 fe_sec_voltage_t fe_voltage;
435 var_Get( p_access, "dvb-voltage", &val );
436 msg_Dbg( p_access, "using voltage=%d", val.i_int );
440 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
441 case 13: fe_voltage = SEC_VOLTAGE_13; break;
442 case 18: fe_voltage = SEC_VOLTAGE_18; break;
444 fe_voltage = SEC_VOLTAGE_OFF;
445 msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
451 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
454 fe_sec_tone_mode_t fe_tone;
456 var_Get( p_access, "dvb-tone", &val );
457 msg_Dbg( p_access, "using tone=%d", val.i_int );
461 case 0: fe_tone = SEC_TONE_OFF; break;
462 case 1: fe_tone = SEC_TONE_ON; break;
464 fe_tone = SEC_TONE_OFF;
465 msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
473 struct dvb_diseqc_master_cmd cmd;
477 static int DoDiseqc( access_t *p_access )
479 access_sys_t *p_sys = p_access->p_sys;
480 frontend_t * p_frontend = p_sys->p_frontend;
482 int i_frequency, i_lnb_slof;
483 fe_sec_voltage_t fe_voltage;
484 fe_sec_tone_mode_t fe_tone;
487 var_Get( p_access, "dvb-frequency", &val );
488 i_frequency = val.i_int;
489 var_Get( p_access, "dvb-lnb-slof", &val );
490 i_lnb_slof = val.i_int;
492 var_Get( p_access, "dvb-tone", &val );
493 if( val.i_int == -1 /* auto */ )
495 if( i_frequency >= i_lnb_slof )
499 var_Set( p_access, "dvb-tone", val );
502 fe_voltage = DecodeVoltage( p_access );
503 fe_tone = DecodeTone( p_access );
505 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
507 msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
508 fe_voltage, i_err, strerror(errno) );
512 var_Get( p_access, "dvb-satno", &val );
515 /* digital satellite equipment control,
516 * specification is available from http://www.eutelsat.com/
518 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
519 SEC_TONE_OFF )) < 0 )
521 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
522 i_err, strerror(errno) );
528 if( val.i_int >= 1 && val.i_int <= 4 )
530 /* 1.x compatible equipment */
531 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
533 /* param: high nibble: reset bits, low nibble set bits,
534 * bits are: option, position, polarization, band
536 cmd.cmd.msg[3] = 0xf0 /* reset bits */
537 | (((val.i_int - 1) * 4) & 0xc)
538 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
539 | (fe_tone == SEC_TONE_ON ? 1 : 0);
541 if( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
544 msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
545 i_err, strerror(errno) );
549 msleep(cmd.wait * 1000);
553 /* A or B simple diseqc */
554 if( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
555 val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
557 msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
558 i_err, strerror(errno) );
566 if( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
568 msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
569 fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
578 static int FrontendSetQPSK( access_t *p_access )
580 access_sys_t *p_sys = p_access->p_sys;
581 frontend_t * p_frontend = p_sys->p_frontend;
582 struct dvb_frontend_parameters fep;
585 int i_frequency, i_lnb_slof;
587 /* Prepare the fep structure */
588 var_Get( p_access, "dvb-frequency", &val );
589 i_frequency = val.i_int;
590 var_Get( p_access, "dvb-lnb-slof", &val );
591 i_lnb_slof = val.i_int;
593 if( i_frequency >= i_lnb_slof )
594 var_Get( p_access, "dvb-lnb-lof2", &val );
596 var_Get( p_access, "dvb-lnb-lof1", &val );
597 fep.frequency = i_frequency - val.i_int;
599 fep.inversion = DecodeInversion( p_access );
601 var_Get( p_access, "dvb-srate", &val );
602 fep.u.qpsk.symbol_rate = val.i_int;
604 var_Get( p_access, "dvb-fec", &val );
605 fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
607 if( DoDiseqc( p_access ) < 0 )
614 /* Empty the event queue */
617 struct dvb_frontend_event event;
618 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
622 /* Now send it all to the frontend device */
623 if( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
625 msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
630 return FrontendCheck( p_access );
633 /*****************************************************************************
634 * FrontendSetQAM : controls the FE device
635 *****************************************************************************/
636 static int FrontendSetQAM( access_t *p_access )
638 access_sys_t *p_sys = p_access->p_sys;
639 frontend_t * p_frontend = p_sys->p_frontend;
640 struct dvb_frontend_parameters fep;
644 /* Prepare the fep structure */
646 var_Get( p_access, "dvb-frequency", &val );
647 fep.frequency = val.i_int;
649 fep.inversion = DecodeInversion( p_access );
651 var_Get( p_access, "dvb-srate", &val );
652 fep.u.qam.symbol_rate = val.i_int;
654 var_Get( p_access, "dvb-fec", &val );
655 fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
657 fep.u.qam.modulation = DecodeModulation( p_access );
659 /* Empty the event queue */
662 struct dvb_frontend_event event;
663 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
667 /* Now send it all to the frontend device */
668 if( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
670 msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
675 return FrontendCheck( p_access );
678 /*****************************************************************************
679 * FrontendSetOFDM : controls the FE device
680 *****************************************************************************/
681 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
684 fe_bandwidth_t fe_bandwidth = 0;
686 var_Get( p_access, "dvb-bandwidth", &val );
687 msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
691 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
692 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
693 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
694 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
696 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
697 fe_bandwidth = BANDWIDTH_AUTO;
703 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
706 fe_transmit_mode_t fe_transmission = 0;
708 var_Get( p_access, "dvb-transmission", &val );
709 msg_Dbg( p_access, "using transmission=%d", val.i_int );
713 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
714 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
715 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
717 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
718 fe_transmission = TRANSMISSION_MODE_AUTO;
721 return fe_transmission;
724 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
727 fe_guard_interval_t fe_guard = 0;
729 var_Get( p_access, "dvb-guard", &val );
730 msg_Dbg( p_access, "using guard=%d", val.i_int );
734 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
735 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
736 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
737 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
738 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
740 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
741 fe_guard = GUARD_INTERVAL_AUTO;
747 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
750 fe_hierarchy_t fe_hierarchy = 0;
752 var_Get( p_access, "dvb-hierarchy", &val );
753 msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
757 case -1: fe_hierarchy = HIERARCHY_NONE; break;
758 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
759 case 1: fe_hierarchy = HIERARCHY_1; break;
760 case 2: fe_hierarchy = HIERARCHY_2; break;
761 case 4: fe_hierarchy = HIERARCHY_4; break;
763 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
764 fe_hierarchy = HIERARCHY_AUTO;
770 static int FrontendSetOFDM( access_t * p_access )
772 access_sys_t *p_sys = p_access->p_sys;
773 frontend_t * p_frontend = p_sys->p_frontend;
774 struct dvb_frontend_parameters fep;
778 /* Prepare the fep structure */
780 var_Get( p_access, "dvb-frequency", &val );
781 fep.frequency = val.i_int;
783 fep.inversion = DecodeInversion( p_access );
785 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
786 var_Get( p_access, "dvb-code-rate-hp", &val );
787 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
788 var_Get( p_access, "dvb-code-rate-lp", &val );
789 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
790 fep.u.ofdm.constellation = DecodeModulation( p_access );
791 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
792 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
793 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
795 /* Empty the event queue */
798 struct dvb_frontend_event event;
799 if( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
803 /* Now send it all to the frontend device */
804 if( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
806 msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
811 return FrontendCheck( p_access );
814 /******************************************************************
815 * FrontendCheck: Check completion of the frontend control sequence
816 ******************************************************************/
817 static int FrontendCheck( access_t * p_access )
819 access_sys_t *p_sys = p_access->p_sys;
820 frontend_t * p_frontend = p_sys->p_frontend;
823 while ( !p_access->b_die && !p_access->b_error )
826 if( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
829 msg_Err( p_access, "reading frontend status failed (%d) %s",
830 i_ret, strerror(errno) );
834 if(status & FE_HAS_SIGNAL) /* found something above the noise level */
835 msg_Dbg(p_access, "check frontend ... has signal");
837 if(status & FE_HAS_CARRIER) /* found a DVB signal */
838 msg_Dbg(p_access, "check frontend ... has carrier");
840 if(status & FE_HAS_VITERBI) /* FEC is stable */
841 msg_Dbg(p_access, "check frontend ... has stable forward error correction");
843 if(status & FE_HAS_SYNC) /* found sync bytes */
844 msg_Dbg(p_access, "check frontend ... has sync");
846 if(status & FE_HAS_LOCK) /* everything's working... */
849 msg_Dbg(p_access, "check frontend ... has lock");
850 msg_Dbg(p_access, "tuning succeeded");
852 /* Read some statistics */
854 if( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
855 msg_Dbg( p_access, "Bit error rate: %d", value );
858 if( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
859 msg_Dbg( p_access, "Signal strength: %d", value );
862 if( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
863 msg_Dbg( p_access, "SNR: %d", value );
868 if(status & FE_TIMEDOUT) /* no lock within the last ~2 seconds */
870 msg_Err(p_access, "tuning failed ... timed out");
874 if(status & FE_REINIT)
876 /* frontend was reinitialized, */
877 /* application is recommended to reset */
878 /* DiSEqC, tone and parameters */
879 msg_Err(p_access, "tuning failed ... resend frontend parameters");
893 /*****************************************************************************
894 * DMXSetFilter : controls the demux to add a filter
895 *****************************************************************************/
896 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
898 struct dmx_pes_filter_params s_filter_params;
900 unsigned int i_adapter, i_device;
904 var_Get( p_access, "dvb-adapter", &val );
905 i_adapter = val.i_int;
906 var_Get( p_access, "dvb-device", &val );
907 i_device = val.i_int;
909 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
910 >= (int)sizeof(dmx) )
912 msg_Err( p_access, "snprintf() truncated string for DMX" );
913 dmx[sizeof(dmx) - 1] = '\0';
916 msg_Dbg( p_access, "Opening device %s", dmx );
917 if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
919 msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
924 /* We fill the DEMUX structure : */
925 s_filter_params.pid = i_pid;
926 s_filter_params.input = DMX_IN_FRONTEND;
927 s_filter_params.output = DMX_OUT_TS_TAP;
928 s_filter_params.flags = DMX_IMMEDIATE_START;
933 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
934 s_filter_params.pes_type = DMX_PES_VIDEO0;
937 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
938 s_filter_params.pes_type = DMX_PES_AUDIO0;
941 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
942 s_filter_params.pes_type = DMX_PES_TELETEXT0;
945 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
946 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
949 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
950 s_filter_params.pes_type = DMX_PES_PCR0;
954 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
955 s_filter_params.pes_type = DMX_PES_VIDEO1;
958 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
959 s_filter_params.pes_type = DMX_PES_AUDIO1;
962 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
963 s_filter_params.pes_type = DMX_PES_TELETEXT1;
966 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
967 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
970 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
971 s_filter_params.pes_type = DMX_PES_PCR1;
975 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
976 s_filter_params.pes_type = DMX_PES_VIDEO2;
979 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
980 s_filter_params.pes_type = DMX_PES_AUDIO2;
983 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
984 s_filter_params.pes_type = DMX_PES_TELETEXT2;
987 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
988 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
991 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
992 s_filter_params.pes_type = DMX_PES_PCR2;
996 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
997 s_filter_params.pes_type = DMX_PES_VIDEO3;
1000 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1001 s_filter_params.pes_type = DMX_PES_AUDIO3;
1004 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1005 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1008 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1009 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1012 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1013 s_filter_params.pes_type = DMX_PES_PCR3;
1015 /* Usually used by Nova cards */
1018 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1019 s_filter_params.pes_type = DMX_PES_OTHER;
1023 /* We then give the order to the device : */
1024 if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1026 msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1028 return VLC_EGENERIC;
1033 /*****************************************************************************
1034 * DMXUnsetFilter : removes a filter
1035 *****************************************************************************/
1036 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1040 if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1042 msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1043 i_ret, strerror(errno) );
1047 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1057 /*****************************************************************************
1059 *****************************************************************************/
1060 int E_(DVROpen)( access_t * p_access )
1062 access_sys_t *p_sys = p_access->p_sys;
1063 unsigned int i_adapter, i_device;
1067 var_Get( p_access, "dvb-adapter", &val );
1068 i_adapter = val.i_int;
1069 var_Get( p_access, "dvb-device", &val );
1070 i_device = val.i_int;
1072 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1073 >= (int)sizeof(dvr) )
1075 msg_Err( p_access, "snprintf() truncated string for DVR" );
1076 dvr[sizeof(dvr) - 1] = '\0';
1079 msg_Dbg( p_access, "Opening device %s", dvr );
1080 if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1082 msg_Err( p_access, "DVROpen: opening device failed (%s)",
1084 return VLC_EGENERIC;
1087 if ( ioctl( p_sys->i_handle, DMX_SET_BUFFER_SIZE, DMX_BUFFER_SIZE ) < 0 )
1089 msg_Warn( p_access, "couldn't set DMX_BUFFER_SIZE (%s)",
1096 /*****************************************************************************
1098 *****************************************************************************/
1099 void E_(DVRClose)( access_t * p_access )
1101 access_sys_t *p_sys = p_access->p_sys;
1103 close( p_sys->i_handle );
1111 /*****************************************************************************
1113 *****************************************************************************/
1114 int E_(CAMOpen)( access_t *p_access )
1116 access_sys_t *p_sys = p_access->p_sys;
1118 int i_adapter, i_device, i_slot;
1121 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1122 i_device = var_GetInteger( p_access, "dvb-device" );
1124 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1126 msg_Err( p_access, "snprintf() truncated string for CA" );
1127 ca[sizeof(ca) - 1] = '\0';
1130 msg_Dbg( p_access, "Opening device %s", ca );
1131 if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1133 msg_Err( p_access, "CAMInit: opening device failed (%s)",
1135 p_sys->i_ca_handle = 0;
1136 return VLC_EGENERIC;
1139 if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1140 || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1142 msg_Err( p_access, "CAMInit: no compatible CAM module" );
1143 close( p_sys->i_ca_handle );
1144 p_sys->i_ca_handle = 0;
1145 return VLC_EGENERIC;
1148 p_sys->i_nb_slots = caps.slot_num;
1149 memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1151 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1153 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1155 msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1159 msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1160 p_sys->i_nb_slots );
1162 p_sys->i_ca_timeout = 100000;
1163 /* Wait a bit otherwise it doesn't initialize properly... */
1169 /*****************************************************************************
1171 *****************************************************************************/
1172 int E_(CAMPoll)( access_t * p_access )
1174 access_sys_t *p_sys = p_access->p_sys;
1176 if ( p_sys->i_ca_handle == 0 )
1178 return VLC_EGENERIC;
1181 return E_(en50221_Poll)( p_access );
1184 /*****************************************************************************
1186 *****************************************************************************/
1187 int E_(CAMSet)( access_t * p_access, uint8_t **pp_capmts, int i_nb_capmts )
1189 access_sys_t *p_sys = p_access->p_sys;
1191 if ( p_sys->i_ca_handle == 0 )
1193 return VLC_EGENERIC;
1196 E_(en50221_SetCAPMT)( p_access, pp_capmts, i_nb_capmts );
1201 /*****************************************************************************
1203 *****************************************************************************/
1204 void E_(CAMClose)( access_t * p_access )
1206 access_sys_t *p_sys = p_access->p_sys;
1208 E_(en50221_End)( p_access );
1210 if ( p_sys->i_ca_handle )
1212 close( p_sys->i_ca_handle );