]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
dvb/access.c - properly cast memory returned by malloc()
[vlc] / modules / access / dvb / linux_dvb.c
1 /*****************************************************************************
2  * dvb.c : functions to control a DVB card under Linux with v4l2
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <sys/ioctl.h>
31 #include <stdio.h>
32 #ifdef HAVE_ERRNO_H
33 #    include <string.h>
34 #    include <errno.h>
35 #endif
36
37 #ifdef HAVE_INTTYPES_H
38 #   include <inttypes.h>                                       /* int16_t .. */
39 #endif
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/poll.h>
48
49 /* DVB Card Drivers */
50 #include <linux/dvb/version.h>
51 #include <linux/dvb/dmx.h>
52 #include <linux/dvb/frontend.h>
53
54 #include <linux/errno.h>
55
56 #include "dvb.h"
57
58
59 /*
60  * Frontends
61  */
62
63 typedef struct frontend_t {
64     int i_handle;
65     struct dvb_frontend_info info;
66 } frontend_t;
67
68 /* Local prototypes */
69 static int FrontendInfo( input_thread_t * p_input );
70 static int FrontendSetQPSK( input_thread_t * p_input );
71 static int FrontendSetQAM( input_thread_t * p_input );
72 static int FrontendSetOFDM( input_thread_t * p_input );
73 static int FrontendCheck( input_thread_t * p_input );
74
75 /*****************************************************************************
76  * FrontendOpen : Determine frontend device information and capabilities
77  *****************************************************************************/
78 int E_(FrontendOpen)( input_thread_t * p_input )
79 {
80     thread_dvb_data_t * p_dvb
81                 = (thread_dvb_data_t *)p_input->p_access_data;
82     frontend_t * p_frontend;
83     unsigned int i_adapter, i_device;
84     vlc_bool_t b_probe;
85     char frontend[128];
86     vlc_value_t val;
87
88     var_Get( p_input, "dvb-adapter", &val );
89     i_adapter = val.i_int;
90     var_Get( p_input, "dvb-device", &val );
91     i_device = val.i_int;
92     var_Get( p_input, "dvb-probe", &val );
93     b_probe = val.b_bool;
94
95     if ( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device )
96             >= (int)sizeof(frontend) )
97     {
98         msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
99         frontend[sizeof(frontend) - 1] = '\0';
100     }
101
102     p_frontend = (frontend_t *) malloc(sizeof(frontend_t));
103     if( p_frontend == NULL )
104     {
105         msg_Err( p_input, "FrontEndOpen: out of memory" );
106         return -1;
107     }
108
109     p_dvb->p_frontend = p_frontend;
110
111     msg_Dbg( p_input, "Opening device %s", frontend );
112     if ( (p_frontend->i_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
113     {
114         msg_Err( p_input, "FrontEndOpen: opening device failed (%s)",
115                  strerror(errno) );
116         free( p_frontend );
117         return -1;
118     }
119
120     if ( b_probe )
121     {
122         char * psz_expected = NULL;
123         char * psz_real;
124
125         if ( FrontendInfo( p_input ) < 0 )
126         {
127             close( p_frontend->i_handle );
128             free( p_frontend );
129             return -1;
130         }
131
132         switch ( p_frontend->info.type )
133         {
134         case FE_OFDM:
135             psz_real = "DVB-T";
136             break;
137         case FE_QAM:
138             psz_real = "DVB-C";
139             break;
140         case FE_QPSK:
141             psz_real = "DVB-S";
142             break;
143         default:
144             psz_real = "unknown";
145         }
146     
147         /* Sanity checks */
148         if( ((strncmp( p_input->psz_access, "qpsk", 4 ) == 0) ||
149              (strncmp( p_input->psz_access, "dvb-s", 5 ) == 0) ||
150              (strncmp( p_input->psz_access, "satellite", 9 ) == 0) ) &&
151              (p_frontend->info.type != FE_QPSK) )
152         {
153             psz_expected = "DVB-S";
154         }
155         if( ((strncmp( p_input->psz_access, "cable", 5 ) == 0) ||
156              (strncmp( p_input->psz_access, "dvb-c", 5 ) == 0) ) &&
157              (p_frontend->info.type != FE_QAM) )
158         {
159             psz_expected = "DVB-C";
160         }
161         if( ((strncmp( p_input->psz_access, "terrestrial", 11 ) == 0) ||
162              (strncmp( p_input->psz_access, "dvb-t", 5 ) == 0) ) &&
163              (p_frontend->info.type != FE_OFDM) )
164         {
165             psz_expected = "DVB-T";
166         }
167
168         if ( psz_expected != NULL )
169         {
170             msg_Err( p_input, "the user asked for %s, and the tuner is %s",
171                      psz_expected, psz_real );
172             close( p_frontend->i_handle );
173             free( p_frontend );
174             return -1;
175         }
176     }
177     else /* no frontend probing is done so use default border values. */
178     {
179         msg_Dbg( p_input, "using default values for frontend info" );
180
181         msg_Dbg( p_input, "method of access is %s", p_input->psz_access );
182         p_frontend->info.type = FE_QPSK;
183         if ( !strncmp( p_input->psz_access, "qpsk", 4 ) ||
184                 !strncmp( p_input->psz_access, "dvb-s", 5 ) )
185             p_frontend->info.type = FE_QPSK;
186         else if ( !strncmp( p_input->psz_access, "cable", 5 ) ||
187                     !strncmp( p_input->psz_access, "dvb-c", 5 ) )
188             p_frontend->info.type = FE_QAM;
189         else if ( !strncmp( p_input->psz_access, "terrestrial", 11 ) ||
190                     !strncmp( p_input->psz_access, "dvb-t", 5 ) )
191             p_frontend->info.type = FE_OFDM;
192     }
193
194     return 0;
195 }
196
197 /*****************************************************************************
198  * FrontendClose : Close the frontend
199  *****************************************************************************/
200 void E_(FrontendClose)( input_thread_t * p_input )
201 {
202     thread_dvb_data_t * p_dvb
203                 = (thread_dvb_data_t *)p_input->p_access_data;
204     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
205
206     if ( p_frontend != NULL )
207     {
208         close( p_frontend->i_handle );
209         free( p_frontend );
210     }
211 }
212
213 /*****************************************************************************
214  * FrontendSet : Tune !
215  *****************************************************************************/
216 int E_(FrontendSet)( input_thread_t * p_input )
217 {
218     thread_dvb_data_t * p_dvb
219                 = (thread_dvb_data_t *)p_input->p_access_data;
220     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
221
222     switch ( p_frontend->info.type )
223     {
224     /* DVB-S: satellite and budget cards (nova) */
225     case FE_QPSK:
226         if ( FrontendSetQPSK( p_input ) < 0 )
227         {
228             msg_Err( p_input, "DVB-S: tuning failed" );
229             return -1;
230         }
231         break;
232
233     /* DVB-C */
234     case FE_QAM:
235         if ( FrontendSetQAM( p_input ) < 0 )
236         {
237             msg_Err( p_input, "DVB-C: tuning failed" );
238             return -1;
239         }
240         break;
241
242     /* DVB-T */
243     case FE_OFDM:
244         if ( FrontendSetOFDM( p_input ) < 0 )
245         {
246             msg_Err( p_input, "DVB-T: tuning failed" );
247             return -1;
248         }
249         break;
250
251     default:
252         msg_Err( p_input, "Could not determine frontend type on %s",
253                  p_frontend->info.name );
254         return -1;
255     }
256     return 0;
257 }
258
259 /*****************************************************************************
260  * FrontendInfo : Return information about given frontend
261  *****************************************************************************/
262 static int FrontendInfo( input_thread_t * p_input )
263 {
264     thread_dvb_data_t * p_dvb
265                 = (thread_dvb_data_t *)p_input->p_access_data;
266     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
267     int i_ret;
268
269     /* Determine type of frontend */
270     if ( (i_ret = ioctl( p_frontend->i_handle, FE_GET_INFO,
271                          &p_frontend->info )) < 0 )
272     {
273         msg_Err( p_input, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
274                  strerror(errno) );
275         return -1;
276     }
277
278     /* Print out frontend capabilities. */
279     msg_Dbg(p_input, "Frontend Info:" );
280     msg_Dbg(p_input, "  name = %s", p_frontend->info.name);
281     switch ( p_frontend->info.type )
282     {
283         case FE_QPSK:
284             msg_Dbg( p_input, "  type = QPSK (DVB-S)" );
285             break;
286         case FE_QAM:
287             msg_Dbg( p_input, "  type = QAM (DVB-C)" );
288             break;
289         case FE_OFDM:
290             msg_Dbg( p_input, "  type = OFDM (DVB-T)" );
291             break;
292 #if 0 /* DVB_API_VERSION == 3 */
293         case FE_MEMORY:
294             msg_Dbg(p_input, "  type = MEMORY" );
295             break;
296         case FE_NET:
297             msg_Dbg(p_input, "  type = NETWORK" );
298             break;
299 #endif
300         default:
301             msg_Err( p_input, "  unknown frontend type (%d)",
302                      p_frontend->info.type );
303             return -1;
304     }
305     msg_Dbg(p_input, "  frequency_min = %u (kHz)",
306             p_frontend->info.frequency_min);
307     msg_Dbg(p_input, "  frequency_max = %u (kHz)",
308             p_frontend->info.frequency_max);
309     msg_Dbg(p_input, "  frequency_stepsize = %u",
310             p_frontend->info.frequency_stepsize);
311     msg_Dbg(p_input, "  frequency_tolerance = %u",
312             p_frontend->info.frequency_tolerance);
313     msg_Dbg(p_input, "  symbol_rate_min = %u (kHz)",
314             p_frontend->info.symbol_rate_min);
315     msg_Dbg(p_input, "  symbol_rate_max = %u (kHz)",
316             p_frontend->info.symbol_rate_max);
317     msg_Dbg(p_input, "  symbol_rate_tolerance (ppm) = %u",
318             p_frontend->info.symbol_rate_tolerance);
319     msg_Dbg(p_input, "  notifier_delay (ms) = %u",
320             p_frontend->info.notifier_delay );
321
322     msg_Dbg(p_input, "Frontend Info capability list:");
323     if (p_frontend->info.caps & FE_IS_STUPID)
324         msg_Dbg(p_input, "  no capabilities - frontend is stupid!");
325     if (p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
326         msg_Dbg(p_input, "  inversion auto");
327     if (p_frontend->info.caps & FE_CAN_FEC_1_2)
328         msg_Dbg(p_input, "  forward error correction 1/2");
329     if (p_frontend->info.caps & FE_CAN_FEC_2_3)
330         msg_Dbg(p_input, "  forward error correction 2/3");
331     if (p_frontend->info.caps & FE_CAN_FEC_3_4)
332         msg_Dbg(p_input, "  forward error correction 3/4");
333     if (p_frontend->info.caps & FE_CAN_FEC_4_5)
334         msg_Dbg(p_input, "  forward error correction 4/5");
335     if (p_frontend->info.caps & FE_CAN_FEC_5_6)
336         msg_Dbg(p_input, "  forward error correction 5/6");
337     if (p_frontend->info.caps & FE_CAN_FEC_6_7)
338         msg_Dbg(p_input, "  forward error correction 6/7");
339     if (p_frontend->info.caps & FE_CAN_FEC_7_8)
340         msg_Dbg(p_input, "  forward error correction 7/8");
341     if (p_frontend->info.caps & FE_CAN_FEC_8_9)
342         msg_Dbg(p_input, "  forward error correction 8/9");
343     if (p_frontend->info.caps & FE_CAN_FEC_AUTO)
344         msg_Dbg(p_input, "  forward error correction auto");
345     if (p_frontend->info.caps & FE_CAN_QPSK)
346         msg_Dbg(p_input, "  card can do QPSK");
347     if (p_frontend->info.caps & FE_CAN_QAM_16)
348         msg_Dbg(p_input, "  card can do QAM 16");
349     if (p_frontend->info.caps & FE_CAN_QAM_32)
350         msg_Dbg(p_input, "  card can do QAM 32");
351     if (p_frontend->info.caps & FE_CAN_QAM_64)
352         msg_Dbg(p_input, "  card can do QAM 64");
353     if (p_frontend->info.caps & FE_CAN_QAM_128)
354         msg_Dbg(p_input, "  card can do QAM 128");
355     if (p_frontend->info.caps & FE_CAN_QAM_256)
356         msg_Dbg(p_input, "  card can do QAM 256");
357     if (p_frontend->info.caps & FE_CAN_QAM_AUTO)
358         msg_Dbg(p_input, "  card can do QAM auto");
359     if (p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
360         msg_Dbg(p_input, "  transmission mode auto");
361     if (p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
362         msg_Dbg(p_input, "  bandwidth mode auto");
363     if (p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
364         msg_Dbg(p_input, "  guard interval mode auto");
365     if (p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
366         msg_Dbg(p_input, "  hierarchy mode auto");
367     if (p_frontend->info.caps & FE_CAN_MUTE_TS)
368         msg_Dbg(p_input, "  card can mute TS");
369     if (p_frontend->info.caps & FE_CAN_CLEAN_SETUP)
370         msg_Dbg(p_input, "  clean setup");
371     msg_Dbg(p_input, "End of capability list");
372
373     return 0;
374 }
375
376 /*****************************************************************************
377  * Decoding the DVB parameters (common)
378  *****************************************************************************/
379 static fe_spectral_inversion_t DecodeInversion( input_thread_t * p_input )
380 {
381     vlc_value_t         val;
382     fe_spectral_inversion_t fe_inversion = 0;
383
384     var_Get( p_input, "dvb-inversion", &val );
385     msg_Dbg( p_input, "using inversion=%d", val.i_int );
386
387     switch ( val.i_int )
388     {
389         case 0: fe_inversion = INVERSION_OFF; break;
390         case 1: fe_inversion = INVERSION_ON; break;
391         case 2: fe_inversion = INVERSION_AUTO; break;
392         default:
393             msg_Dbg( p_input, "dvb has inversion not set, using auto");
394             fe_inversion = INVERSION_AUTO;
395             break;
396     }
397     return fe_inversion;
398 }
399
400 static fe_code_rate_t DecodeFEC( input_thread_t * p_input, int i_val )
401 {
402     fe_code_rate_t      fe_fec = FEC_NONE;
403
404     msg_Dbg( p_input, "using feq=%d", i_val );
405
406     switch ( i_val )
407     {
408         case 1: fe_fec = FEC_1_2; break;
409         case 2: fe_fec = FEC_2_3; break;
410         case 3: fe_fec = FEC_3_4; break;
411         case 4: fe_fec = FEC_4_5; break;
412         case 5: fe_fec = FEC_5_6; break;
413         case 6: fe_fec = FEC_6_7; break;
414         case 7: fe_fec = FEC_7_8; break;
415         case 8: fe_fec = FEC_8_9; break;
416         case 9: fe_fec = FEC_AUTO; break;
417         default:
418             /* cannot happen */
419             fe_fec = FEC_NONE;
420             msg_Err( p_input, "argument has invalid FEC (%d)", i_val);
421             break;
422     }    
423     return fe_fec;
424 }
425
426 static fe_modulation_t DecodeModulation( input_thread_t * p_input )
427 {
428     vlc_value_t         val;
429     fe_modulation_t     fe_modulation = 0;
430
431     var_Get( p_input, "dvb-modulation", &val );
432
433     switch ( val.i_int )
434     {
435         case -1: fe_modulation = QPSK; break;
436         case 0: fe_modulation = QAM_AUTO; break;
437         case 16: fe_modulation = QAM_16; break;
438         case 32: fe_modulation = QAM_32; break;
439         case 64: fe_modulation = QAM_64; break;
440         case 128: fe_modulation = QAM_128; break;
441         case 256: fe_modulation = QAM_256; break;
442         default:
443             msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
444             fe_modulation = QAM_AUTO;
445             break;
446     }    
447     return fe_modulation;
448 }
449
450 /*****************************************************************************
451  * FrontendSetQPSK : controls the FE device
452  *****************************************************************************/
453 static fe_sec_voltage_t DecodeVoltage( input_thread_t * p_input )
454 {
455     vlc_value_t         val;
456     fe_sec_voltage_t    fe_voltage;
457
458     var_Get( p_input, "dvb-voltage", &val );
459     msg_Dbg( p_input, "using voltage=%d", val.i_int );
460
461     switch ( val.i_int )
462     {
463         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
464         case 13: fe_voltage = SEC_VOLTAGE_13; break;
465         case 18: fe_voltage = SEC_VOLTAGE_18; break;
466         default:
467             fe_voltage = SEC_VOLTAGE_OFF;
468             msg_Err( p_input, "argument has invalid voltage (%d)", val.i_int);
469             break;
470     }    
471     return fe_voltage;
472 }
473
474 static fe_sec_tone_mode_t DecodeTone( input_thread_t * p_input )
475 {
476     vlc_value_t         val;
477     fe_sec_tone_mode_t  fe_tone;
478
479     var_Get( p_input, "dvb-tone", &val );
480     msg_Dbg( p_input, "using tone=%d", val.i_int );
481
482     switch ( val.i_int )
483     {
484         case 0: fe_tone = SEC_TONE_OFF; break;
485         case 1: fe_tone = SEC_TONE_ON; break;
486         default:
487             fe_tone = SEC_TONE_OFF;
488             msg_Err( p_input, "argument has invalid tone mode (%d)", val.i_int);
489             break;
490     }    
491     return fe_tone;
492 }
493
494 struct diseqc_cmd_t
495 {
496     struct dvb_diseqc_master_cmd cmd;
497     uint32_t wait;
498 };
499
500 static int DoDiseqc( input_thread_t * p_input )
501 {
502     thread_dvb_data_t * p_dvb
503                 = (thread_dvb_data_t *)p_input->p_access_data;
504     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
505     vlc_value_t val;
506     int i_frequency, i_lnb_slof;
507     fe_sec_voltage_t fe_voltage;
508     fe_sec_tone_mode_t fe_tone;
509     int i_err;
510
511     var_Get( p_input, "dvb-frequency", &val );
512     i_frequency = val.i_int;
513     var_Get( p_input, "dvb-lnb-slof", &val );
514     i_lnb_slof = val.i_int;
515
516     var_Get( p_input, "dvb-tone", &val );
517     if ( val.i_int == -1 /* auto */ )
518     {
519         if ( i_frequency >= i_lnb_slof )
520             val.i_int = 1;
521         else
522             val.i_int = 0;
523         var_Set( p_input, "dvb-tone", val );
524     }
525
526     fe_voltage = DecodeVoltage( p_input );
527     fe_tone = DecodeTone( p_input );
528
529     if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
530     {
531         msg_Err( p_input, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
532                  fe_voltage, i_err, strerror(errno) );
533         return i_err;
534     }
535
536     var_Get( p_input, "dvb-satno", &val );
537     if ( val.i_int != 0 )
538     {
539         /* digital satellite equipment control,
540          * specification is available from http://www.eutelsat.com/ 
541          */
542         if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
543                              SEC_TONE_OFF )) < 0 )
544         {
545             msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
546                      i_err, strerror(errno) );
547             return i_err;
548         }
549
550         msleep(15000);
551
552         if ( val.i_int >= 1 && val.i_int <= 4 )
553         {
554             /* 1.x compatible equipment */
555             struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
556
557             /* param: high nibble: reset bits, low nibble set bits,
558              * bits are: option, position, polarization, band
559              */
560             cmd.cmd.msg[3] = 0xf0 /* reset bits */
561                               | (((val.i_int - 1) * 4) & 0xc)
562                               | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
563                               | (fe_tone == SEC_TONE_ON ? 1 : 0);
564
565             if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
566                                &cmd.cmd )) < 0 )
567             {
568                 msg_Err( p_input, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
569                          i_err, strerror(errno) );
570                 return i_err;
571             }
572
573             msleep(cmd.wait * 1000);
574         }
575         else
576         {
577             /* A or B simple diseqc */
578             if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
579                           val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
580             {
581                 msg_Err( p_input, "ioctl FE_SEND_BURST failed (%d) %s",
582                          i_err, strerror(errno) );
583                 return i_err;
584             }
585         }
586
587         msleep(15000);
588     }
589
590     if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
591     {
592         msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
593                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
594                  strerror(errno) );
595         return i_err;
596     }
597
598     msleep(15000);
599     return 0;
600 }
601
602 static int FrontendSetQPSK( input_thread_t * p_input )
603 {
604     thread_dvb_data_t * p_dvb
605                 = (thread_dvb_data_t *)p_input->p_access_data;
606     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
607     struct dvb_frontend_parameters fep;
608     int i_ret;
609     vlc_value_t val;
610     int i_frequency, i_lnb_slof;
611
612     /* Prepare the fep structure */
613
614     var_Get( p_input, "dvb-frequency", &val );
615     i_frequency = val.i_int;
616     var_Get( p_input, "dvb-lnb-slof", &val );
617     i_lnb_slof = val.i_int;
618
619     if ( i_frequency >= i_lnb_slof )
620         var_Get( p_input, "dvb-lnb-lof2", &val );
621     else
622         var_Get( p_input, "dvb-lnb-lof1", &val );
623     fep.frequency = i_frequency - val.i_int;
624
625     fep.inversion = DecodeInversion( p_input );
626
627     var_Get( p_input, "dvb-srate", &val );
628     fep.u.qpsk.symbol_rate = val.i_int;
629
630     var_Get( p_input, "dvb-fec", &val );
631     fep.u.qpsk.fec_inner = DecodeFEC( p_input, val.i_int );
632
633     if ( DoDiseqc( p_input ) < 0 )
634     {
635         return -1;
636     }
637
638     msleep(100000);
639
640     /* Empty the event queue */
641     for ( ; ; )
642     {
643         struct dvb_frontend_event event;
644         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
645             break;
646     }
647
648     /* Now send it all to the frontend device */
649     if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
650     {
651         msg_Err( p_input, "DVB-S: setting frontend failed (%d) %s", i_ret,
652                  strerror(errno) );
653         return -1;
654     }
655
656     return FrontendCheck( p_input );
657 }
658
659 /*****************************************************************************
660  * FrontendSetQAM : controls the FE device
661  *****************************************************************************/
662 static int FrontendSetQAM( input_thread_t * p_input )
663 {
664     thread_dvb_data_t * p_dvb
665                 = (thread_dvb_data_t *)p_input->p_access_data;
666     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
667     struct dvb_frontend_parameters fep;
668     vlc_value_t val;
669     int i_ret;
670
671     /* Prepare the fep structure */
672
673     var_Get( p_input, "dvb-frequency", &val );
674     fep.frequency = val.i_int;
675
676     fep.inversion = DecodeInversion( p_input );
677
678     var_Get( p_input, "dvb-srate", &val );
679     fep.u.qam.symbol_rate = val.i_int;
680
681     var_Get( p_input, "dvb-fec", &val );
682     fep.u.qam.fec_inner = DecodeFEC( p_input, val.i_int );
683
684     fep.u.qam.modulation = DecodeModulation( p_input );
685
686     /* Empty the event queue */
687     for ( ; ; )
688     {
689         struct dvb_frontend_event event;
690         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
691             break;
692     }
693
694     /* Now send it all to the frontend device */
695     if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
696     {
697         msg_Err( p_input, "DVB-C: setting frontend failed (%d) %s", i_ret,
698                  strerror(errno) );
699         return -1;
700     }
701
702     return FrontendCheck( p_input );
703 }
704
705 /*****************************************************************************
706  * FrontendSetOFDM : controls the FE device
707  *****************************************************************************/
708 static fe_bandwidth_t DecodeBandwidth( input_thread_t * p_input )
709 {
710     vlc_value_t         val;
711     fe_bandwidth_t      fe_bandwidth = 0;
712
713     var_Get( p_input, "dvb-bandwidth", &val );
714     msg_Dbg( p_input, "using bandwidth=%d", val.i_int );
715
716     switch ( val.i_int )
717     {
718         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
719         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
720         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
721         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
722         default:
723             msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto" );
724             fe_bandwidth = BANDWIDTH_AUTO;
725             break;
726     }
727     return fe_bandwidth;
728 }
729
730 static fe_transmit_mode_t DecodeTransmission( input_thread_t * p_input )
731 {
732     vlc_value_t         val;
733     fe_transmit_mode_t  fe_transmission = 0;
734
735     var_Get( p_input, "dvb-transmission", &val );
736     msg_Dbg( p_input, "using transmission=%d", val.i_int );
737
738     switch ( val.i_int )
739     {
740         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
741         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
742         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
743         default:
744             msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
745             fe_transmission = TRANSMISSION_MODE_AUTO;
746             break;
747     }    
748     return fe_transmission;
749 }
750
751 static fe_guard_interval_t DecodeGuardInterval( input_thread_t * p_input )
752 {
753     vlc_value_t         val;
754     fe_guard_interval_t fe_guard = 0;
755
756     var_Get( p_input, "dvb-guard", &val );
757     msg_Dbg( p_input, "using guard=%d", val.i_int );
758
759     switch ( val.i_int )
760     {
761         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
762         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
763         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
764         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
765         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
766         default:
767             msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
768             fe_guard = GUARD_INTERVAL_AUTO;
769             break;
770     }
771     return fe_guard;
772 }
773
774 static fe_hierarchy_t DecodeHierarchy( input_thread_t * p_input )
775 {
776     vlc_value_t         val;
777     fe_hierarchy_t      fe_hierarchy = 0;
778
779     var_Get( p_input, "dvb-hierarchy", &val );
780     msg_Dbg( p_input, "using hierarchy=%d", val.i_int );
781
782     switch ( val.i_int )
783     {
784         case -1: fe_hierarchy = HIERARCHY_NONE; break;
785         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
786         case 1: fe_hierarchy = HIERARCHY_1; break;
787         case 2: fe_hierarchy = HIERARCHY_2; break;
788         case 4: fe_hierarchy = HIERARCHY_4; break;
789         default:
790             msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
791             fe_hierarchy = HIERARCHY_AUTO;
792             break;
793     }
794     return fe_hierarchy;
795 }
796
797 static int FrontendSetOFDM( input_thread_t * p_input )
798 {
799     thread_dvb_data_t * p_dvb
800                 = (thread_dvb_data_t *)p_input->p_access_data;
801     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
802     struct dvb_frontend_parameters fep;
803     vlc_value_t val;
804     int ret;
805
806     /* Prepare the fep structure */
807
808     var_Get( p_input, "dvb-frequency", &val );
809     fep.frequency = val.i_int;
810
811     fep.inversion = DecodeInversion( p_input );
812
813     fep.u.ofdm.bandwidth = DecodeBandwidth( p_input );
814     var_Get( p_input, "dvb-code-rate-HP", &val );
815     fep.u.ofdm.code_rate_HP = DecodeFEC( p_input, val.i_int );
816     var_Get( p_input, "dvb-code-rate-LP", &val );
817     fep.u.ofdm.code_rate_LP = DecodeFEC( p_input, val.i_int );
818     fep.u.ofdm.constellation = DecodeModulation( p_input );
819     fep.u.ofdm.transmission_mode = DecodeTransmission( p_input );
820     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_input );
821     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_input );
822
823     /* Empty the event queue */
824     for ( ; ; )
825     {
826         struct dvb_frontend_event event;
827         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
828             break;
829     }
830
831     /* Now send it all to the frontend device */
832     if ( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
833     {
834         msg_Err( p_input, "DVB-T: setting frontend failed (%d) %s", ret,
835                  strerror(errno) );
836         return -1;
837     }
838
839     return FrontendCheck( p_input );
840 }
841
842 /******************************************************************
843  * FrontendCheck: Check completion of the frontend control sequence
844  ******************************************************************/
845 static int FrontendCheck( input_thread_t * p_input )
846 {
847     thread_dvb_data_t * p_dvb
848                 = (thread_dvb_data_t *)p_input->p_access_data;
849     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
850     int i_ret;
851
852     while ( !p_input->b_die && !p_input->b_error )
853     {
854         fe_status_t status;
855         if ( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
856                              &status )) < 0 )
857         {
858             msg_Err( p_input, "reading frontend status failed (%d) %s",
859                      i_ret, strerror(errno) );
860             return i_ret;
861         }
862
863         if (status & FE_HAS_SIGNAL) /* found something above the noise level */
864             msg_Dbg(p_input, "check frontend ... has signal");
865
866         if (status & FE_HAS_CARRIER) /* found a DVB signal  */
867             msg_Dbg(p_input, "check frontend ... has carrier");
868
869         if (status & FE_HAS_VITERBI) /* FEC is stable  */
870             msg_Dbg(p_input, "check frontend ... has stable forward error correction");
871
872         if (status & FE_HAS_SYNC)    /* found sync bytes  */
873             msg_Dbg(p_input, "check frontend ... has sync");
874
875         if (status & FE_HAS_LOCK)    /* everything's working... */
876         {
877             int32_t value;
878             msg_Dbg(p_input, "check frontend ... has lock");
879             msg_Dbg(p_input, "tuning succeeded");
880
881             /* Read some statistics */
882             value = 0;
883             if ( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
884                 msg_Dbg( p_input, "Bit error rate: %d", value );
885
886             value = 0;
887             if ( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
888                 msg_Dbg( p_input, "Signal strength: %d", value );
889
890             value = 0;
891             if ( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
892                 msg_Dbg( p_input, "SNR: %d", value );
893
894             return 0;
895         }
896
897         if (status & FE_TIMEDOUT)    /*  no lock within the last ~2 seconds */
898         {
899             msg_Err(p_input, "tuning failed ... timed out");
900             return -2;
901         }
902
903         if (status & FE_REINIT)
904         {
905             /*  frontend was reinitialized,  */
906             /*  application is recommended to reset */
907             /*  DiSEqC, tone and parameters */
908             msg_Err(p_input, "tuning failed ... resend frontend parameters");
909             return -3;
910         }
911
912         msleep(500000);
913     }
914     return -1;
915 }
916
917
918 /*
919  * Demux
920  */
921
922 /*****************************************************************************
923  * DMXSetFilter : controls the demux to add a filter
924  *****************************************************************************/
925 int E_(DMXSetFilter)( input_thread_t * p_input, int i_pid, int * pi_fd,
926                       int i_type )
927 {
928     struct dmx_pes_filter_params s_filter_params;
929     int i_ret;
930     unsigned int i_adapter, i_device;
931     char dmx[128];
932     vlc_value_t val;
933
934     var_Get( p_input, "dvb-adapter", &val );
935     i_adapter = val.i_int;
936     var_Get( p_input, "dvb-device", &val );
937     i_device = val.i_int;
938
939     if ( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
940             >= (int)sizeof(dmx) )
941     {
942         msg_Err( p_input, "snprintf() truncated string for DMX" );
943         dmx[sizeof(dmx) - 1] = '\0';
944     }
945
946     msg_Dbg( p_input, "Opening device %s", dmx );
947     if ( (*pi_fd = open(dmx, O_RDWR)) < 0 )
948     {
949         msg_Err( p_input, "DMXSetFilter: opening device failed (%s)",
950                  strerror(errno) );
951         return -1;
952     }
953
954     /* We fill the DEMUX structure : */
955     s_filter_params.pid     =   i_pid;
956     s_filter_params.input   =   DMX_IN_FRONTEND;
957     s_filter_params.output  =   DMX_OUT_TS_TAP;
958     s_filter_params.flags   =   DMX_IMMEDIATE_START;
959
960     switch ( i_type )
961     {   /* First device */
962         case 1:
963             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
964             s_filter_params.pes_type = DMX_PES_VIDEO0;
965             break;
966         case 2:
967             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
968             s_filter_params.pes_type = DMX_PES_AUDIO0;
969             break;
970         case 3:
971             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
972             s_filter_params.pes_type = DMX_PES_TELETEXT0;
973             break;
974         case 4:
975             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
976             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
977             break;
978         case 5:
979             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
980             s_filter_params.pes_type = DMX_PES_PCR0;
981             break;
982         /* Second device */
983         case 6:
984             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
985             s_filter_params.pes_type = DMX_PES_VIDEO1;
986             break;
987         case 7:
988             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
989             s_filter_params.pes_type = DMX_PES_AUDIO1;
990             break;
991         case 8:
992             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
993             s_filter_params.pes_type = DMX_PES_TELETEXT1;
994             break;
995         case 9:
996             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
997             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
998             break;
999         case 10:
1000             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1001             s_filter_params.pes_type = DMX_PES_PCR1;
1002             break;
1003         /* Third device */
1004         case 11:
1005             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1006             s_filter_params.pes_type = DMX_PES_VIDEO2;
1007             break;
1008         case 12:
1009             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1010             s_filter_params.pes_type = DMX_PES_AUDIO2;
1011             break;
1012         case 13:
1013             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1014             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1015             break;
1016         case 14:
1017             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1018             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1019             break;
1020         case 15:
1021             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1022             s_filter_params.pes_type = DMX_PES_PCR2;
1023             break;
1024         /* Forth device */
1025         case 16:
1026             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1027             s_filter_params.pes_type = DMX_PES_VIDEO3;
1028             break;
1029         case 17:
1030             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1031             s_filter_params.pes_type = DMX_PES_AUDIO3;
1032             break;
1033         case 18:
1034             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1035             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1036             break;
1037         case 19:
1038             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1039             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1040             break;
1041         case 20:
1042             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1043             s_filter_params.pes_type = DMX_PES_PCR3;
1044             break;
1045         /* Usually used by Nova cards */
1046         case 21:
1047         default:
1048             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1049             s_filter_params.pes_type = DMX_PES_OTHER;
1050             break;
1051     }
1052
1053     /* We then give the order to the device : */
1054     if ( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1055     {
1056         msg_Err( p_input, "DMXSetFilter: failed with %d (%s)", i_ret,
1057                  strerror(errno) );
1058         return -1;
1059     }
1060     return 0;
1061 }
1062
1063 /*****************************************************************************
1064  * DMXUnsetFilter : removes a filter
1065  *****************************************************************************/
1066 int E_(DMXUnsetFilter)( input_thread_t * p_input, int i_fd )
1067 {
1068     int i_ret;
1069
1070     if ( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1071     {
1072         msg_Err( p_input, "DMX_STOP failed for demux (%d) %s",
1073                  i_ret, strerror(errno) );
1074         return i_ret;
1075     }
1076
1077     msg_Dbg( p_input, "DMXUnsetFilter: closing demux %d", i_fd);
1078     close( i_fd );
1079     return 0;
1080 }
1081
1082
1083 /*
1084  * DVR device
1085  */
1086
1087 /*****************************************************************************
1088  * DVROpen :
1089  *****************************************************************************/
1090 int E_(DVROpen)( input_thread_t * p_input )
1091 {
1092     thread_dvb_data_t * p_dvb
1093                 = (thread_dvb_data_t *)p_input->p_access_data;
1094     unsigned int i_adapter, i_device;
1095     char dvr[128];
1096     vlc_value_t val;
1097
1098     var_Get( p_input, "dvb-adapter", &val );
1099     i_adapter = val.i_int;
1100     var_Get( p_input, "dvb-device", &val );
1101     i_device = val.i_int;
1102
1103     if ( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1104             >= (int)sizeof(dvr) )
1105     {
1106         msg_Err( p_input, "snprintf() truncated string for DVR" );
1107         dvr[sizeof(dvr) - 1] = '\0';
1108     }
1109
1110     msg_Dbg( p_input, "Opening device %s", dvr );
1111     if ( (p_dvb->i_handle = open(dvr, O_RDONLY)) < 0 )
1112     {
1113         msg_Err( p_input, "DVROpen: opening device failed (%s)",
1114                  strerror(errno) );
1115         return -1;
1116     }
1117
1118     return 0;
1119 }
1120
1121 /*****************************************************************************
1122  * DVRClose :
1123  *****************************************************************************/
1124 void E_(DVRClose)( input_thread_t * p_input )
1125 {
1126     thread_dvb_data_t * p_dvb
1127                 = (thread_dvb_data_t *)p_input->p_access_data;
1128
1129     close( p_dvb->i_handle );
1130 }
1131