1 /*****************************************************************************
2 * access.c: DVB card input v4l2 only
3 *****************************************************************************
4 * Copyright (C) 1998-2003 VideoLAN
6 * Authors: Johan Bilien <jobi@via.ecp.fr>
7 * Jean-Paul Saman <jpsaman@wxs.nl>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
34 #include "../../demux/mpeg/system.h"
41 #include <sys/types.h>
48 #ifdef STRNCASECMP_IN_STRINGS_H
52 /* DVB Card Drivers */
53 #include <linux/dvb/dmx.h>
54 #include <linux/dvb/frontend.h>
58 #define SATELLITE_READ_ONCE 3
60 /*****************************************************************************
62 *****************************************************************************/
63 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
65 static int SatelliteSetArea ( input_thread_t *, input_area_t * );
66 static int SatelliteSetProgram ( input_thread_t *, pgrm_descriptor_t * );
67 static void SatelliteSeek ( input_thread_t *, off_t );
69 /*****************************************************************************
70 * Open: open the frontend device
71 *****************************************************************************/
72 int E_(Open) ( vlc_object_t *p_this )
74 struct dvb_frontend_info frontend_info;
75 struct dvb_frontend_parameters fep;
76 input_thread_t * p_input = (input_thread_t *)p_this;
77 input_socket_t * p_satellite;
81 unsigned int u_adapter = 1;
82 unsigned int u_device = 0;
83 unsigned int u_freq = 0;
84 unsigned int u_srate = 0;
85 unsigned int u_lnb_lof1;
86 unsigned int u_lnb_lof2;
87 unsigned int u_lnb_slof;
91 int i_transmission = 0;
93 vlc_bool_t b_polarisation = 0;
95 int i_code_rate_HP = 0;
96 int i_code_rate_LP = 0;
100 char frontend[] = FRONTEND;
106 /* parse the options passed in command line : */
107 psz_parser = strdup( p_input->psz_name );
114 // Get adapter and device number to use for this dvb card
115 u_adapter = config_GetInt( p_input, "adapter" );
116 u_device = config_GetInt( p_input, "device" );
118 /* Get antenna configuration options */
119 b_diseqc = config_GetInt( p_input, "diseqc" );
120 u_lnb_lof1 = config_GetInt( p_input, "lnb-lof1" );
121 u_lnb_lof2 = config_GetInt( p_input, "lnb-lof2" );
122 u_lnb_slof = config_GetInt( p_input, "lnb-slof" );
124 /*Get modulation parameters*/
125 i_bandwidth = config_GetInt( p_input, "bandwidth");
126 i_code_rate_HP = config_GetInt(p_input, "code-rate-hp");
127 i_code_rate_LP = config_GetInt(p_input, "code-rate-lp");
128 i_modulation = config_GetInt(p_input, "modulation");
129 i_transmission = config_GetInt(p_input, "transmission");
130 i_guard = config_GetInt(p_input, "guard");
131 i_hierarchy = config_GetInt(p_input, "hierarchy");
134 /* Determine frontend device information and capabilities */
135 b_probe = config_GetInt( p_input, "probe" );
138 if ( ioctl_InfoFrontend(p_input, &frontend_info, u_adapter, u_device) < 0 )
140 msg_Err( p_input, "(access) cannot determine frontend info" );
144 else /* no frontend probing is done so use default border values. */
146 msg_Dbg( p_input, "using default values for frontend info" );
147 i_len = sizeof(FRONTEND);
148 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
150 msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
151 frontend[sizeof(FRONTEND)] = '\0';
153 strncpy(frontend_info.name, frontend, 128);
155 msg_Dbg(p_input, "method of access is %s", p_input->psz_access);
157 frontend_info.type = FE_QPSK;
158 if (strncmp( p_input->psz_access, "qpsk",4 ) ==0)
159 frontend_info.type = FE_QPSK;
160 else if (strncmp( p_input->psz_access, "cable",5 ) ==0)
161 frontend_info.type = FE_QAM;
162 else if (strncmp( p_input->psz_access, "terrestrial",11) ==0)
163 frontend_info.type = FE_OFDM;
165 // frontend_info.frequency_max = 12999000; /* in KHz, lnb_lof2 */
166 // frontend_info.frequency_min = 9750000; /* lnb_lof1 */
168 frontend_info.frequency_max = u_lnb_lof2; /* in KHz, lnb_lof2 */
169 frontend_info.frequency_min = u_lnb_lof1; /* lnb_lof1 */
171 frontend_info.symbol_rate_max = 30000000;
172 frontend_info.symbol_rate_min = 1000000;
175 /* Register Callback functions */
176 p_input->pf_read = SatelliteRead;
177 p_input->pf_set_program = SatelliteSetProgram;
178 p_input->pf_set_area = SatelliteSetArea;
179 p_input->pf_seek = SatelliteSeek;
181 i_test = strtol( psz_parser, &psz_next, 10 );
182 if ( psz_next == psz_parser )
186 if( !strncmp( psz_parser, "frequency=",
187 strlen( "frequency=" ) ) )
190 strtol( psz_parser + strlen( "frequency=" ),
193 else if( !strncmp( psz_parser, "polarization=",
194 strlen( "polarization=" ) ) )
196 char *psz_parser_init;
197 psz_parser += strlen( "polarization=" );
198 psz_parser_init = psz_parser;
199 while ( *psz_parser != ':')
203 if ((!strncmp( psz_parser_init, "V" ,
204 psz_parser - psz_parser_init ) ) ||
205 (!strncmp( psz_parser_init, "V" ,
206 psz_parser - psz_parser_init ) ) )
208 b_polarisation = VLC_FALSE;
210 else if ((!strncmp( psz_parser_init, "H" ,
211 psz_parser - psz_parser_init ) ) ||
212 (!strncmp( psz_parser_init, "h" ,
213 psz_parser - psz_parser_init ) ) )
216 b_polarisation = VLC_TRUE;
219 else if( !strncmp( psz_parser, "fec=",
223 (int)strtol( psz_parser + strlen( "fec=" ),
226 else if( !strncmp( psz_parser, "srate=",
227 strlen( "srate=" ) ) )
230 (unsigned int)strtol( psz_parser + strlen( "srate=" ),
233 else if( !strncmp( psz_parser, "program=",
234 strlen( "program=" ) ) )
236 val.i_int = (int)strtol( psz_parser + strlen( "program=" ),
238 var_Set( p_input, "program", val );
240 else if( !strncmp( psz_parser, "diseqc",
241 strlen( "disecq" ) ) )
243 psz_parser += strlen("disecq");
246 else if( !strncmp( psz_parser, "lnb-lof1=",
247 strlen( "lnb-lof1=" ) ) )
250 (unsigned int)strtol( psz_parser + strlen( "lnb_lof1=" ),
253 else if( !strncmp( psz_parser, "lnb-lof2=",
254 strlen( "lnb-lof2=" ) ) )
257 (unsigned int)strtol( psz_parser + strlen( "lnb_lof2=" ),
260 else if( !strncmp( psz_parser, "lnb-slof=",
261 strlen( "lnb-slof=" ) ) )
264 (unsigned int)strtol( psz_parser + strlen( "lnb_slof=" ),
267 else if( !strncmp( psz_parser, "device=",
268 strlen( "device=" ) ) )
271 (unsigned int)strtol( psz_parser + strlen( "device=" ),
274 else if( !strncmp( psz_parser, "adapter=",
275 strlen( "adapter=" ) ) )
278 (unsigned int)strtol( psz_parser + strlen( "adapter=" ),
281 else if (!strncmp( psz_parser, "modulation=",
282 strlen( "modulation=" ) ) )
284 i_modulation = (int)strtol( psz_parser + strlen( "modulation=" ),
287 else if (!strncmp( psz_parser, "bandwidth=",
288 strlen( "bandwidth=" ) ) )
290 i_bandwidth = (int)strtol( psz_parser + strlen( "bandwidth=" ),
293 else if (!strncmp( psz_parser, "guard=",
294 strlen( "guard=" ) ) )
296 i_guard = (int)strtol( psz_parser + strlen( "guard=" ),
299 else if (!strncmp( psz_parser, "transmission=",
300 strlen( "transmission=" ) ) )
302 i_transmission = (int)strtol( psz_parser +
303 strlen( "transmission=" ),&psz_parser, 0 );
305 else if (!strncmp( psz_parser, "hierarchy=",
306 strlen( "hierarchy=" ) ) )
308 i_hierarchy = (int)strtol( psz_parser +
309 strlen( "hierarchy=" ),&psz_parser, 0 );
311 else if (!strncmp( psz_parser, "code-rate-HP=",
312 strlen( "code-rate-HP=" ) ) )
314 i_code_rate_HP = (int)strtol( psz_parser +
315 strlen( "code-rate-HP=" ),&psz_parser, 0 );
317 else if (!strncmp( psz_parser, "code-rate-LP=",
318 strlen( "code-rate-LP=" ) ) )
320 i_code_rate_LP = (int)strtol( psz_parser +
321 strlen( "code-rate-LP=" ),&psz_parser, 0 );
323 else if( !strncmp( psz_parser, "probe",
324 strlen( "probe" ) ) )
326 psz_parser += strlen("probe");
337 msg_Warn(p_input,"DVB Input syntax has changed, please see documentation for further informations");
338 u_freq = (unsigned int)i_test;
341 psz_parser = psz_next + 1;
342 b_polarisation = (vlc_bool_t)strtol( psz_parser, &psz_next, 10 );
345 psz_parser = psz_next + 1;
346 i_fec = (int)strtol( psz_parser, &psz_next, 10 );
349 psz_parser = psz_next + 1;
350 u_srate = (unsigned int)strtol( psz_parser, &psz_next, 10 );
356 /* Validating input values */
357 if ( ((u_freq) > frontend_info.frequency_max) ||
358 ((u_freq) < frontend_info.frequency_min) )
360 msg_Warn( p_input, "invalid frequency %d (KHz), using default one", u_freq );
361 u_freq = config_GetInt( p_input, "frequency" );
362 if ( ((u_freq) > frontend_info.frequency_max) ||
363 ((u_freq) < frontend_info.frequency_min) )
365 msg_Err( p_input, "invalid default frequency" );
370 /* Workaround for backwards compatibility */
371 if (strncmp( p_input->psz_access, "satellite", 9 ) ==0)
373 msg_Warn( p_input, "invalid symbol rate %d possibly specified in MHz, trying value *1000 KHz", u_freq );
377 if ( ((u_srate) > frontend_info.symbol_rate_max) ||
378 ((u_srate) < frontend_info.symbol_rate_min) )
380 msg_Warn( p_input, "invalid symbol rate, using default one" );
381 u_srate = config_GetInt( p_input, "symbol-rate" );
382 if ( ((u_srate) > frontend_info.symbol_rate_max) ||
383 ((u_srate) < frontend_info.symbol_rate_min) )
385 msg_Err( p_input, "invalid default symbol rate" );
390 if( b_polarisation && (b_polarisation != 1) )
392 msg_Warn( p_input, "invalid polarization, using default one" );
393 b_polarisation = config_GetInt( p_input, "polarization" );
394 if( b_polarisation && b_polarisation != 1 )
396 msg_Err( p_input, "invalid default polarization" );
401 if( (i_fec > 9) || (i_fec < 1) )
403 msg_Warn( p_input, "invalid FEC, using default one" );
404 i_fec = config_GetInt( p_input, "fec" );
405 if( (i_fec > 9) || (i_fec < 1) )
407 msg_Err( p_input, "invalid default FEC" );
412 /* Setting frontend parameters for tuning the hardware */
413 msg_Dbg( p_input, "Trying to tune to channel ...");
414 switch( frontend_info.type )
416 /* DVB-S: satellite and budget cards (nova) */
418 fep.frequency = u_freq; /* KHz */
419 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
420 fep.u.qpsk.symbol_rate = u_srate;
421 fep.u.qpsk.fec_inner = dvb_DecodeFEC(p_input, i_fec);
422 msg_Dbg( p_input, "satellite (QPSK) frontend found on %s", frontend_info.name );
424 if (ioctl_SetQPSKFrontend (p_input, fep, b_polarisation,
425 u_lnb_lof1, u_lnb_lof2, u_lnb_slof,
426 u_adapter, u_device )<0)
428 msg_Err( p_input, "DVB-S frontend returned a failure event" );
435 fep.frequency = u_freq; /* KHz */
436 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
437 fep.u.qam.symbol_rate = u_srate;
438 fep.u.qam.fec_inner = dvb_DecodeFEC(p_input, i_fec);
439 fep.u.qam.modulation = dvb_DecodeModulation(p_input, i_modulation);
440 msg_Dbg( p_input, "cable (QAM) frontend found on %s", frontend_info.name );
441 if (ioctl_SetQAMFrontend (p_input, fep, u_adapter, u_device )<0)
443 msg_Err( p_input, "DVB-C frontend returned a failure event" );
450 fep.frequency = u_freq; /* KHz */
451 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
452 fep.u.ofdm.bandwidth = dvb_DecodeBandwidth(p_input, i_bandwidth);
453 fep.u.ofdm.code_rate_HP = dvb_DecodeFEC(p_input, i_code_rate_HP);
454 fep.u.ofdm.code_rate_LP = dvb_DecodeFEC(p_input, i_code_rate_LP);
455 fep.u.ofdm.constellation = dvb_DecodeModulation(p_input, i_modulation);
456 fep.u.ofdm.transmission_mode = dvb_DecodeTransmission(p_input, i_transmission);
457 fep.u.ofdm.guard_interval = dvb_DecodeGuardInterval(p_input, i_guard);
458 fep.u.ofdm.hierarchy_information = dvb_DecodeHierarchy(p_input, i_hierarchy);
459 msg_Dbg( p_input, "terrestrial (OFDM) frontend found on %s", frontend_info.name );
460 if (ioctl_SetOFDMFrontend (p_input, fep,u_adapter, u_device )<0)
462 msg_Err( p_input, "DVB-T frontend returned a failure event" );
467 msg_Err( p_input, "Could not determine frontend type on %s", frontend_info.name );
470 msg_Dbg( p_input, "Tuning done.");
472 /* Initialise structure */
473 p_satellite = malloc( sizeof( input_socket_t ) );
475 if( p_satellite == NULL )
477 msg_Err( p_input, "out of memory" );
481 p_input->p_access_data = (void *)p_satellite;
483 /* Open the DVR device */
485 if (snprintf(dvr, sizeof(DVR), DVR, u_adapter, u_device) >= i_len)
487 msg_Err( p_input, "snprintf() truncated string for DVR" );
488 dvr[sizeof(DVR)] = '\0';
490 msg_Dbg( p_input, "opening DVR device '%s'", dvr );
492 if( (p_satellite->i_handle = open( dvr,
493 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
496 msg_Warn( p_input, "cannot open `%s' (%s)", dvr, strerror(errno) );
498 msg_Warn( p_input, "cannot open `%s'", dvr );
504 msg_Dbg( p_input, "setting filter on PAT" );
506 /* Set Filter on PAT packet */
507 if ( ioctl_SetDMXFilter(p_input, 0, &i_fd, 21, u_adapter, u_device ) < 0 )
510 msg_Err( p_input, "an error occured when setting filter on PAT (%s)", strerror(errno) );
512 msg_Err( p_input, "an error occured when setting filter on PAT" );
514 close( p_satellite->i_handle );
519 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
521 msg_Err( p_input, "could not initialize stream structure" );
522 close( p_satellite->i_handle );
527 vlc_mutex_lock( &p_input->stream.stream_lock );
529 p_input->stream.b_pace_control = 1;
530 p_input->stream.b_seekable = 0;
531 p_input->stream.p_selected_area->i_tell = 0;
533 vlc_mutex_unlock( &p_input->stream.stream_lock );
535 p_input->i_mtu = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
536 p_input->stream.i_method = INPUT_METHOD_SATELLITE;
541 /*****************************************************************************
542 * Close : Close the device
543 *****************************************************************************/
544 void E_(Close) ( vlc_object_t *p_this )
546 input_thread_t * p_input = (input_thread_t *)p_this;
547 input_socket_t * p_satellite;
548 unsigned int i_es_index;
550 if ( p_input->stream.p_selected_program )
552 for ( i_es_index = 1 ;
553 i_es_index < p_input->stream.p_selected_program->i_es_number;
556 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
557 if ( p_es->p_decoder_fifo )
559 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
565 p_satellite = (input_socket_t *)p_input;
566 close( p_satellite->i_handle );
569 /*****************************************************************************
570 * SatelliteRead: reads data from the satellite card
571 *****************************************************************************/
572 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
575 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
577 unsigned int u_adapter = 1;
578 unsigned int u_device = 0;
581 // Get adapter and device number to use for this dvb card
582 u_adapter = config_GetInt( p_input, "adapter" );
583 u_device = config_GetInt( p_input, "device" );
585 /* if not set, set filters to the PMTs */
586 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
588 if ( p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd == 0 )
590 ioctl_SetDMXFilter(p_input, p_input->stream.pp_programs[i]->pp_es[0]->i_id,
591 &p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd,
592 21, u_adapter, u_device );
596 i_ret = read( p_access_data->i_handle, p_buffer, i_len );
601 msg_Err( p_input, "read failed (%s)", strerror(errno) );
603 msg_Err( p_input, "read failed" );
610 /*****************************************************************************
611 * SatelliteSetArea : Does nothing
612 *****************************************************************************/
613 static int SatelliteSetArea( input_thread_t * p_input, input_area_t * p_area )
618 /*****************************************************************************
619 * SatelliteSetProgram : Sets the card filters according to the
621 * and makes the appropriate changes to stream structure.
622 *****************************************************************************/
623 int SatelliteSetProgram( input_thread_t * p_input,
624 pgrm_descriptor_t * p_new_prg )
626 unsigned int i_es_index;
628 unsigned int u_adapter = 1;
629 unsigned int u_device = 0;
630 unsigned int u_video_type = 1; /* default video type */
631 unsigned int u_audio_type = 2; /* default audio type */
633 /* Get adapter and device number to use for this dvb card */
634 u_adapter = config_GetInt( p_input, "adapter" );
635 u_device = config_GetInt( p_input, "device" );
637 if ( p_input->stream.p_selected_program )
639 for ( i_es_index = 1 ; /* 0 should be the PMT */
640 i_es_index < p_input->stream.p_selected_program->i_es_number ;
643 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
644 if ( p_es->p_decoder_fifo )
646 input_UnselectES( p_input , p_es );
648 if ( p_es->i_demux_fd > 0)
650 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
651 p_es->i_demux_fd = 0;
657 for (i_es_index = 1 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
659 #define p_es p_new_prg->pp_es[i_es_index]
660 switch( p_es->i_cat )
664 case MPEG2_MOTO_VIDEO_ES:
665 if ( input_SelectES( p_input , p_es ) == 0 )
667 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_video_type,
668 u_adapter, u_device);
674 if ( input_SelectES( p_input , p_es ) == 0 )
676 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_audio_type,
677 u_adapter, u_device);
678 input_SelectES( p_input , p_es );
683 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, 21, u_adapter, u_device);
684 input_SelectES( p_input , p_es );
685 msg_Warn(p_input, "ES streamtype 0x%d found used as DMX_PES_OTHER !!",(int) p_es->i_cat);
691 p_input->stream.p_selected_program = p_new_prg;
693 /* Update the navigation variables without triggering a callback */
694 val.i_int = p_new_prg->i_number;
695 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
700 /*****************************************************************************
701 * SatelliteSeek: does nothing (not a seekable stream
702 *****************************************************************************/
703 static void SatelliteSeek( input_thread_t * p_input, off_t i_off )