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 <saman@natlab.research.philips.com>
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;
103 /* parse the options passed in command line : */
104 psz_parser = strdup( p_input->psz_name );
111 // Get adapter and device number to use for this dvb card
112 u_adapter = config_GetInt( p_input, "adapter" );
113 u_device = config_GetInt( p_input, "device" );
115 /* Determine frontend device information and capabilities */
116 b_probe = config_GetInt( p_input, "probe" );
119 if ( ioctl_InfoFrontend(p_input, &frontend_info, u_adapter, u_device) < 0 )
121 msg_Err( p_input, "(access) cannot determine frontend info" );
125 else /* no frontend probing is done so use default border values. */
127 msg_Dbg( p_input, "using default values for frontend info" );
128 i_len = sizeof(FRONTEND);
129 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
131 msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
132 frontend[sizeof(FRONTEND)] = '\0';
134 strncpy(frontend_info.name, frontend, 128);
136 msg_Dbg(p_input, "method of access is %s", p_input->psz_access);
138 frontend_info.type = FE_QPSK;
139 if (strncmp( p_input->psz_access, "qpsk",4 ) ==0)
140 frontend_info.type = FE_QPSK;
141 else if (strncmp( p_input->psz_access, "cable",5 ) ==0)
142 frontend_info.type = FE_QAM;
143 else if (strncmp( p_input->psz_access, "terrestrial",11) ==0)
144 frontend_info.type = FE_OFDM;
146 frontend_info.frequency_max = 12999 * 1000;
147 frontend_info.frequency_min = 9750 * 1000;
148 frontend_info.symbol_rate_max = 30000 * 1000;
149 frontend_info.symbol_rate_min = 1000 * 1000;
152 /* Register Callback functions */
153 p_input->pf_read = SatelliteRead;
154 p_input->pf_set_program = SatelliteSetProgram;
155 p_input->pf_set_area = SatelliteSetArea;
156 p_input->pf_seek = SatelliteSeek;
158 u_freq = (unsigned int)strtol( psz_parser, &psz_next, 10 );
161 psz_parser = psz_next + 1;
162 b_polarisation = (vlc_bool_t)strtol( psz_parser, &psz_next, 10 );
165 psz_parser = psz_next + 1;
166 i_fec = (int)strtol( psz_parser, &psz_next, 10 );
169 psz_parser = psz_next + 1;
170 u_srate = (unsigned int)strtol( psz_parser, &psz_next, 10 );
175 /* Validating input values */
177 if ( ((u_freq) > frontend_info.frequency_max) ||
178 ((u_freq) < frontend_info.frequency_min) )
180 msg_Warn( p_input, "invalid frequency %d (kHz), using default one", u_freq );
181 u_freq = config_GetInt( p_input, "frequency" );
183 if ( ((u_freq) > frontend_info.frequency_max) ||
184 ((u_freq) < frontend_info.frequency_min) )
186 msg_Err( p_input, "invalid default frequency" );
192 if ( ((u_srate) > frontend_info.symbol_rate_max) ||
193 ((u_srate) < frontend_info.symbol_rate_min) )
195 msg_Warn( p_input, "invalid symbol rate, using default one" );
196 u_srate = config_GetInt( p_input, "symbol-rate" );
198 if ( ((u_srate) > frontend_info.symbol_rate_max) ||
199 ((u_srate) < frontend_info.symbol_rate_min) )
201 msg_Err( p_input, "invalid default symbol rate" );
206 if( b_polarisation && (b_polarisation != 1) )
208 msg_Warn( p_input, "invalid polarization, using default one" );
209 b_polarisation = config_GetInt( p_input, "polarization" );
210 if( b_polarisation && b_polarisation != 1 )
212 msg_Err( p_input, "invalid default polarization" );
217 if( (i_fec > 9) || (i_fec < 1) )
219 msg_Warn( p_input, "invalid FEC, using default one" );
220 i_fec = config_GetInt( p_input, "fec" );
221 if( (i_fec > 9) || (i_fec < 1) )
223 msg_Err( p_input, "invalid default FEC" );
228 /* Get antenna configuration options */
229 b_diseqc = config_GetInt( p_input, "diseqc" );
230 u_lnb_lof1 = config_GetInt( p_input, "lnb-lof1" ) * 1000;
231 u_lnb_lof2 = config_GetInt( p_input, "lnb-lof2" ) * 1000;
232 u_lnb_slof = config_GetInt( p_input, "lnb-slof" ) * 1000;
234 /* Setting frontend parameters for tuning the hardware */
235 switch( frontend_info.type )
237 /* DVB-S: satellite and budget cards (nova) */
239 fep.frequency = u_freq;
240 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
241 fep.u.qpsk.symbol_rate = u_srate;
242 fep.u.qpsk.fec_inner = dvb_DecodeFEC(p_input, i_fec);
243 msg_Dbg( p_input, "satellite (QPSK) frontend found on %s", frontend_info.name );
248 i_modulation = config_GetInt(p_input, "modulation");
250 fep.frequency = u_freq;
251 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
252 fep.u.qam.symbol_rate = u_srate;
253 fep.u.qam.fec_inner = dvb_DecodeFEC(p_input, i_fec);
254 fep.u.qam.modulation = dvb_DecodeModulation(p_input, i_modulation);
255 msg_Dbg( p_input, "cable (QAM) frontend found on %s", frontend_info.name );
260 i_bandwidth = config_GetInt( p_input, "bandwidth");
261 i_code_rate_HP = config_GetInt(p_input, "code-rate-hp");
262 i_code_rate_LP = config_GetInt(p_input, "code-rate-lp");
263 i_modulation = config_GetInt(p_input, "modulation");
264 i_transmission = config_GetInt(p_input, "transmission");
265 i_guard = config_GetInt(p_input, "guard");
266 i_hierarchy = config_GetInt(p_input, "hierarchy");
268 fep.frequency = u_freq;
269 fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
270 fep.u.ofdm.bandwidth = dvb_DecodeBandwidth(p_input, i_bandwidth);
271 fep.u.ofdm.code_rate_HP = dvb_DecodeFEC(p_input, i_code_rate_HP);
272 fep.u.ofdm.code_rate_LP = dvb_DecodeFEC(p_input, i_code_rate_LP);
273 fep.u.ofdm.constellation = dvb_DecodeModulation(p_input, i_modulation);
274 fep.u.ofdm.transmission_mode = dvb_DecodeTransmission(p_input, i_transmission);
275 fep.u.ofdm.guard_interval = dvb_DecodeGuardInterval(p_input, i_guard);
276 fep.u.ofdm.hierarchy_information = dvb_DecodeHierarchy(p_input, i_hierarchy);
277 msg_Dbg( p_input, "terrestrial (OFDM) frontend found on %s", frontend_info.name );
281 msg_Err( p_input, "Could not determine frontend type on %s", frontend_info.name );
285 /* Initialise structure */
286 p_satellite = malloc( sizeof( input_socket_t ) );
288 if( p_satellite == NULL )
290 msg_Err( p_input, "out of memory" );
294 p_input->p_access_data = (void *)p_satellite;
296 /* Open the DVR device */
298 if (snprintf(dvr, sizeof(DVR), DVR, u_adapter, u_device) >= i_len)
300 msg_Err( p_input, "snprintf() truncated string for DVR" );
301 dvr[sizeof(DVR)] = '\0';
303 msg_Dbg( p_input, "opening DVR device '%s'", dvr );
305 if( (p_satellite->i_handle = open( dvr,
306 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
309 msg_Warn( p_input, "cannot open `%s' (%s)", dvr, strerror(errno) );
311 msg_Warn( p_input, "cannot open `%s'", dvr );
317 /* Initialize the Satellite Card */
318 switch (ioctl_SetFrontend (p_input, fep, b_polarisation,
319 u_lnb_lof1, u_lnb_lof2, u_lnb_slof,
320 u_adapter, u_device ))
323 msg_Err( p_input, "frontend returned an unexpected event" );
324 close( p_satellite->i_handle );
328 msg_Err( p_input, "frontend returned no event" );
329 close( p_satellite->i_handle );
333 msg_Err( p_input, "frontend: timeout when polling for event" );
334 close( p_satellite->i_handle );
338 msg_Err( p_input, "an error occured when polling frontend device" );
339 close( p_satellite->i_handle );
343 msg_Err( p_input, "frontend returned a failure event" );
344 close( p_satellite->i_handle );
351 msg_Dbg( p_input, "setting filter on PAT" );
353 if ( ioctl_SetDMXFilter(p_input, 0, &i_fd, 21, u_adapter, u_device ) < 0 )
356 msg_Err( p_input, "an error occured when setting filter on PAT (%s)", strerror(errno) );
358 msg_Err( p_input, "an error occured when setting filter on PAT" );
360 close( p_satellite->i_handle );
365 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
367 msg_Err( p_input, "could not initialize stream structure" );
368 close( p_satellite->i_handle );
373 vlc_mutex_lock( &p_input->stream.stream_lock );
375 p_input->stream.b_pace_control = 1;
376 p_input->stream.b_seekable = 0;
377 p_input->stream.p_selected_area->i_tell = 0;
379 vlc_mutex_unlock( &p_input->stream.stream_lock );
381 p_input->i_mtu = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
382 p_input->stream.i_method = INPUT_METHOD_SATELLITE;
387 /*****************************************************************************
388 * Close : Close the device
389 *****************************************************************************/
390 void E_(Close) ( vlc_object_t *p_this )
392 input_thread_t * p_input = (input_thread_t *)p_this;
393 input_socket_t * p_satellite;
394 unsigned int i_es_index;
396 if ( p_input->stream.p_selected_program )
398 for ( i_es_index = 1 ;
399 i_es_index < p_input->stream.p_selected_program->i_es_number;
402 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
403 if ( p_es->p_decoder_fifo )
405 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
411 p_satellite = (input_socket_t *)p_input;
412 close( p_satellite->i_handle );
415 /*****************************************************************************
416 * SatelliteRead: reads data from the satellite card
417 *****************************************************************************/
418 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
421 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
423 unsigned int u_adapter = 1;
424 unsigned int u_device = 0;
427 // Get adapter and device number to use for this dvb card
428 u_adapter = config_GetInt( p_input, "adapter" );
429 u_device = config_GetInt( p_input, "device" );
431 /* if not set, set filters to the PMTs */
432 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
434 if ( p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd == 0 )
436 ioctl_SetDMXFilter(p_input, p_input->stream.pp_programs[i]->pp_es[0]->i_id,
437 &p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd,
438 21, u_adapter, u_device );
442 i_ret = read( p_access_data->i_handle, p_buffer, i_len );
447 msg_Err( p_input, "read failed (%s)", strerror(errno) );
449 msg_Err( p_input, "read failed" );
456 /*****************************************************************************
457 * SatelliteSetArea : Does nothing
458 *****************************************************************************/
459 static int SatelliteSetArea( input_thread_t * p_input, input_area_t * p_area )
464 /*****************************************************************************
465 * SatelliteSetProgram : Sets the card filters according to the
467 * and makes the appropriate changes to stream structure.
468 *****************************************************************************/
469 int SatelliteSetProgram( input_thread_t * p_input,
470 pgrm_descriptor_t * p_new_prg )
472 unsigned int i_es_index;
474 unsigned int u_adapter = 1;
475 unsigned int u_device = 0;
476 unsigned int u_video_type = 1; /* default video type */
477 unsigned int u_audio_type = 2; /* default audio type */
479 // Get adapter and device number to use for this dvb card
480 u_adapter = config_GetInt( p_input, "adapter" );
481 u_device = config_GetInt( p_input, "device" );
483 if ( p_input->stream.p_selected_program )
485 for ( i_es_index = 1 ; /* 0 should be the PMT */
486 i_es_index < p_input->stream.p_selected_program->i_es_number ;
489 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
490 if ( p_es->p_decoder_fifo )
492 input_UnselectES( p_input , p_es );
494 if ( p_es->i_demux_fd )
496 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
497 p_es->i_demux_fd = 0;
503 for (i_es_index = 1 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
505 #define p_es p_new_prg->pp_es[i_es_index]
506 switch( p_es->i_cat )
510 case MPEG2_MOTO_VIDEO_ES:
511 if ( input_SelectES( p_input , p_es ) == 0 )
513 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_video_type,
514 u_adapter, u_device);
520 if ( input_SelectES( p_input , p_es ) == 0 )
522 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_audio_type,
523 u_adapter, u_device);
524 input_SelectES( p_input , p_es );
529 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, 21, u_adapter, u_device);
530 input_SelectES( p_input , p_es );
531 msg_Dbg(p_input, "Warning ES streamtype 0x%d found used as DMX_PES_OTHER !!",(int) p_es->i_cat);
537 p_input->stream.p_selected_program = p_new_prg;
539 /* Update the navigation variables without triggering a callback */
540 val.i_int = p_new_prg->i_number;
541 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
546 /*****************************************************************************
547 * SatelliteSeek: does nothing (not a seekable stream
548 *****************************************************************************/
549 static void SatelliteSeek( input_thread_t * p_input, off_t i_off )