]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
FEC_NONE is a valid value in DecodeFEC(). (Fix thanks to Guido Flohr)
[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 fec=%d", i_val );
405
406     switch ( i_val )
407     {
408         case 0: fe_fec = FEC_NONE; break;
409         case 1: fe_fec = FEC_1_2; break;
410         case 2: fe_fec = FEC_2_3; break;
411         case 3: fe_fec = FEC_3_4; break;
412         case 4: fe_fec = FEC_4_5; break;
413         case 5: fe_fec = FEC_5_6; break;
414         case 6: fe_fec = FEC_6_7; break;
415         case 7: fe_fec = FEC_7_8; break;
416         case 8: fe_fec = FEC_8_9; break;
417         case 9: fe_fec = FEC_AUTO; break;
418         default:
419             /* cannot happen */
420             fe_fec = FEC_NONE;
421             msg_Err( p_input, "argument has invalid FEC (%d)", i_val);
422             break;
423     }    
424     return fe_fec;
425 }
426
427 static fe_modulation_t DecodeModulation( input_thread_t * p_input )
428 {
429     vlc_value_t         val;
430     fe_modulation_t     fe_modulation = 0;
431
432     var_Get( p_input, "dvb-modulation", &val );
433
434     switch ( val.i_int )
435     {
436         case -1: fe_modulation = QPSK; break;
437         case 0: fe_modulation = QAM_AUTO; break;
438         case 16: fe_modulation = QAM_16; break;
439         case 32: fe_modulation = QAM_32; break;
440         case 64: fe_modulation = QAM_64; break;
441         case 128: fe_modulation = QAM_128; break;
442         case 256: fe_modulation = QAM_256; break;
443         default:
444             msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
445             fe_modulation = QAM_AUTO;
446             break;
447     }    
448     return fe_modulation;
449 }
450
451 /*****************************************************************************
452  * FrontendSetQPSK : controls the FE device
453  *****************************************************************************/
454 static fe_sec_voltage_t DecodeVoltage( input_thread_t * p_input )
455 {
456     vlc_value_t         val;
457     fe_sec_voltage_t    fe_voltage;
458
459     var_Get( p_input, "dvb-voltage", &val );
460     msg_Dbg( p_input, "using voltage=%d", val.i_int );
461
462     switch ( val.i_int )
463     {
464         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
465         case 13: fe_voltage = SEC_VOLTAGE_13; break;
466         case 18: fe_voltage = SEC_VOLTAGE_18; break;
467         default:
468             fe_voltage = SEC_VOLTAGE_OFF;
469             msg_Err( p_input, "argument has invalid voltage (%d)", val.i_int);
470             break;
471     }    
472     return fe_voltage;
473 }
474
475 static fe_sec_tone_mode_t DecodeTone( input_thread_t * p_input )
476 {
477     vlc_value_t         val;
478     fe_sec_tone_mode_t  fe_tone;
479
480     var_Get( p_input, "dvb-tone", &val );
481     msg_Dbg( p_input, "using tone=%d", val.i_int );
482
483     switch ( val.i_int )
484     {
485         case 0: fe_tone = SEC_TONE_OFF; break;
486         case 1: fe_tone = SEC_TONE_ON; break;
487         default:
488             fe_tone = SEC_TONE_OFF;
489             msg_Err( p_input, "argument has invalid tone mode (%d)", val.i_int);
490             break;
491     }    
492     return fe_tone;
493 }
494
495 struct diseqc_cmd_t
496 {
497     struct dvb_diseqc_master_cmd cmd;
498     uint32_t wait;
499 };
500
501 static int DoDiseqc( input_thread_t * p_input )
502 {
503     thread_dvb_data_t * p_dvb
504                 = (thread_dvb_data_t *)p_input->p_access_data;
505     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
506     vlc_value_t val;
507     int i_frequency, i_lnb_slof;
508     fe_sec_voltage_t fe_voltage;
509     fe_sec_tone_mode_t fe_tone;
510     int i_err;
511
512     var_Get( p_input, "dvb-frequency", &val );
513     i_frequency = val.i_int;
514     var_Get( p_input, "dvb-lnb-slof", &val );
515     i_lnb_slof = val.i_int;
516
517     var_Get( p_input, "dvb-tone", &val );
518     if ( val.i_int == -1 /* auto */ )
519     {
520         if ( i_frequency >= i_lnb_slof )
521             val.i_int = 1;
522         else
523             val.i_int = 0;
524         var_Set( p_input, "dvb-tone", val );
525     }
526
527     fe_voltage = DecodeVoltage( p_input );
528     fe_tone = DecodeTone( p_input );
529
530     if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
531     {
532         msg_Err( p_input, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
533                  fe_voltage, i_err, strerror(errno) );
534         return i_err;
535     }
536
537     var_Get( p_input, "dvb-satno", &val );
538     if ( val.i_int != 0 )
539     {
540         /* digital satellite equipment control,
541          * specification is available from http://www.eutelsat.com/ 
542          */
543         if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE,
544                              SEC_TONE_OFF )) < 0 )
545         {
546             msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=off (%d) %s",
547                      i_err, strerror(errno) );
548             return i_err;
549         }
550
551         msleep(15000);
552
553         if ( val.i_int >= 1 && val.i_int <= 4 )
554         {
555             /* 1.x compatible equipment */
556             struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
557
558             /* param: high nibble: reset bits, low nibble set bits,
559              * bits are: option, position, polarization, band
560              */
561             cmd.cmd.msg[3] = 0xf0 /* reset bits */
562                               | (((val.i_int - 1) * 4) & 0xc)
563                               | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
564                               | (fe_tone == SEC_TONE_ON ? 1 : 0);
565
566             if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_MASTER_CMD,
567                                &cmd.cmd )) < 0 )
568             {
569                 msg_Err( p_input, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
570                          i_err, strerror(errno) );
571                 return i_err;
572             }
573
574             msleep(cmd.wait * 1000);
575         }
576         else
577         {
578             /* A or B simple diseqc */
579             if ( (i_err = ioctl( p_frontend->i_handle, FE_DISEQC_SEND_BURST,
580                           val.i_int == -1 ? SEC_MINI_A : SEC_MINI_B )) < 0 )
581             {
582                 msg_Err( p_input, "ioctl FE_SEND_BURST failed (%d) %s",
583                          i_err, strerror(errno) );
584                 return i_err;
585             }
586         }
587
588         msleep(15000);
589     }
590
591     if ( (i_err = ioctl( p_frontend->i_handle, FE_SET_TONE, fe_tone )) < 0 )
592     {
593         msg_Err( p_input, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
594                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
595                  strerror(errno) );
596         return i_err;
597     }
598
599     msleep(15000);
600     return 0;
601 }
602
603 static int FrontendSetQPSK( input_thread_t * p_input )
604 {
605     thread_dvb_data_t * p_dvb
606                 = (thread_dvb_data_t *)p_input->p_access_data;
607     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
608     struct dvb_frontend_parameters fep;
609     int i_ret;
610     vlc_value_t val;
611     int i_frequency, i_lnb_slof;
612
613     /* Prepare the fep structure */
614
615     var_Get( p_input, "dvb-frequency", &val );
616     i_frequency = val.i_int;
617     var_Get( p_input, "dvb-lnb-slof", &val );
618     i_lnb_slof = val.i_int;
619
620     if ( i_frequency >= i_lnb_slof )
621         var_Get( p_input, "dvb-lnb-lof2", &val );
622     else
623         var_Get( p_input, "dvb-lnb-lof1", &val );
624     fep.frequency = i_frequency - val.i_int;
625
626     fep.inversion = DecodeInversion( p_input );
627
628     var_Get( p_input, "dvb-srate", &val );
629     fep.u.qpsk.symbol_rate = val.i_int;
630
631     var_Get( p_input, "dvb-fec", &val );
632     fep.u.qpsk.fec_inner = DecodeFEC( p_input, val.i_int );
633
634     if ( DoDiseqc( p_input ) < 0 )
635     {
636         return -1;
637     }
638
639     msleep(100000);
640
641     /* Empty the event queue */
642     for ( ; ; )
643     {
644         struct dvb_frontend_event event;
645         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
646             break;
647     }
648
649     /* Now send it all to the frontend device */
650     if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
651     {
652         msg_Err( p_input, "DVB-S: setting frontend failed (%d) %s", i_ret,
653                  strerror(errno) );
654         return -1;
655     }
656
657     return FrontendCheck( p_input );
658 }
659
660 /*****************************************************************************
661  * FrontendSetQAM : controls the FE device
662  *****************************************************************************/
663 static int FrontendSetQAM( input_thread_t * p_input )
664 {
665     thread_dvb_data_t * p_dvb
666                 = (thread_dvb_data_t *)p_input->p_access_data;
667     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
668     struct dvb_frontend_parameters fep;
669     vlc_value_t val;
670     int i_ret;
671
672     /* Prepare the fep structure */
673
674     var_Get( p_input, "dvb-frequency", &val );
675     fep.frequency = val.i_int;
676
677     fep.inversion = DecodeInversion( p_input );
678
679     var_Get( p_input, "dvb-srate", &val );
680     fep.u.qam.symbol_rate = val.i_int;
681
682     var_Get( p_input, "dvb-fec", &val );
683     fep.u.qam.fec_inner = DecodeFEC( p_input, val.i_int );
684
685     fep.u.qam.modulation = DecodeModulation( p_input );
686
687     /* Empty the event queue */
688     for ( ; ; )
689     {
690         struct dvb_frontend_event event;
691         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
692             break;
693     }
694
695     /* Now send it all to the frontend device */
696     if ( (i_ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
697     {
698         msg_Err( p_input, "DVB-C: setting frontend failed (%d) %s", i_ret,
699                  strerror(errno) );
700         return -1;
701     }
702
703     return FrontendCheck( p_input );
704 }
705
706 /*****************************************************************************
707  * FrontendSetOFDM : controls the FE device
708  *****************************************************************************/
709 static fe_bandwidth_t DecodeBandwidth( input_thread_t * p_input )
710 {
711     vlc_value_t         val;
712     fe_bandwidth_t      fe_bandwidth = 0;
713
714     var_Get( p_input, "dvb-bandwidth", &val );
715     msg_Dbg( p_input, "using bandwidth=%d", val.i_int );
716
717     switch ( val.i_int )
718     {
719         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
720         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
721         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
722         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
723         default:
724             msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto" );
725             fe_bandwidth = BANDWIDTH_AUTO;
726             break;
727     }
728     return fe_bandwidth;
729 }
730
731 static fe_transmit_mode_t DecodeTransmission( input_thread_t * p_input )
732 {
733     vlc_value_t         val;
734     fe_transmit_mode_t  fe_transmission = 0;
735
736     var_Get( p_input, "dvb-transmission", &val );
737     msg_Dbg( p_input, "using transmission=%d", val.i_int );
738
739     switch ( val.i_int )
740     {
741         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
742         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
743         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
744         default:
745             msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
746             fe_transmission = TRANSMISSION_MODE_AUTO;
747             break;
748     }    
749     return fe_transmission;
750 }
751
752 static fe_guard_interval_t DecodeGuardInterval( input_thread_t * p_input )
753 {
754     vlc_value_t         val;
755     fe_guard_interval_t fe_guard = 0;
756
757     var_Get( p_input, "dvb-guard", &val );
758     msg_Dbg( p_input, "using guard=%d", val.i_int );
759
760     switch ( val.i_int )
761     {
762         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
763         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
764         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
765         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
766         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
767         default:
768             msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
769             fe_guard = GUARD_INTERVAL_AUTO;
770             break;
771     }
772     return fe_guard;
773 }
774
775 static fe_hierarchy_t DecodeHierarchy( input_thread_t * p_input )
776 {
777     vlc_value_t         val;
778     fe_hierarchy_t      fe_hierarchy = 0;
779
780     var_Get( p_input, "dvb-hierarchy", &val );
781     msg_Dbg( p_input, "using hierarchy=%d", val.i_int );
782
783     switch ( val.i_int )
784     {
785         case -1: fe_hierarchy = HIERARCHY_NONE; break;
786         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
787         case 1: fe_hierarchy = HIERARCHY_1; break;
788         case 2: fe_hierarchy = HIERARCHY_2; break;
789         case 4: fe_hierarchy = HIERARCHY_4; break;
790         default:
791             msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
792             fe_hierarchy = HIERARCHY_AUTO;
793             break;
794     }
795     return fe_hierarchy;
796 }
797
798 static int FrontendSetOFDM( input_thread_t * p_input )
799 {
800     thread_dvb_data_t * p_dvb
801                 = (thread_dvb_data_t *)p_input->p_access_data;
802     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
803     struct dvb_frontend_parameters fep;
804     vlc_value_t val;
805     int ret;
806
807     /* Prepare the fep structure */
808
809     var_Get( p_input, "dvb-frequency", &val );
810     fep.frequency = val.i_int;
811
812     fep.inversion = DecodeInversion( p_input );
813
814     fep.u.ofdm.bandwidth = DecodeBandwidth( p_input );
815     var_Get( p_input, "dvb-code-rate-hp", &val );
816     fep.u.ofdm.code_rate_HP = DecodeFEC( p_input, val.i_int );
817     var_Get( p_input, "dvb-code-rate-lp", &val );
818     fep.u.ofdm.code_rate_LP = DecodeFEC( p_input, val.i_int );
819     fep.u.ofdm.constellation = DecodeModulation( p_input );
820     fep.u.ofdm.transmission_mode = DecodeTransmission( p_input );
821     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_input );
822     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_input );
823
824     /* Empty the event queue */
825     for ( ; ; )
826     {
827         struct dvb_frontend_event event;
828         if ( ioctl( p_frontend->i_handle, FE_GET_EVENT, &event ) < 0 )
829             break;
830     }
831
832     /* Now send it all to the frontend device */
833     if ( (ret = ioctl( p_frontend->i_handle, FE_SET_FRONTEND, &fep )) < 0 )
834     {
835         msg_Err( p_input, "DVB-T: setting frontend failed (%d) %s", ret,
836                  strerror(errno) );
837         return -1;
838     }
839
840     return FrontendCheck( p_input );
841 }
842
843 /******************************************************************
844  * FrontendCheck: Check completion of the frontend control sequence
845  ******************************************************************/
846 static int FrontendCheck( input_thread_t * p_input )
847 {
848     thread_dvb_data_t * p_dvb
849                 = (thread_dvb_data_t *)p_input->p_access_data;
850     frontend_t * p_frontend = (frontend_t *)p_dvb->p_frontend;
851     int i_ret;
852
853     while ( !p_input->b_die && !p_input->b_error )
854     {
855         fe_status_t status;
856         if ( (i_ret = ioctl( p_frontend->i_handle, FE_READ_STATUS,
857                              &status )) < 0 )
858         {
859             msg_Err( p_input, "reading frontend status failed (%d) %s",
860                      i_ret, strerror(errno) );
861             return i_ret;
862         }
863
864         if (status & FE_HAS_SIGNAL) /* found something above the noise level */
865             msg_Dbg(p_input, "check frontend ... has signal");
866
867         if (status & FE_HAS_CARRIER) /* found a DVB signal  */
868             msg_Dbg(p_input, "check frontend ... has carrier");
869
870         if (status & FE_HAS_VITERBI) /* FEC is stable  */
871             msg_Dbg(p_input, "check frontend ... has stable forward error correction");
872
873         if (status & FE_HAS_SYNC)    /* found sync bytes  */
874             msg_Dbg(p_input, "check frontend ... has sync");
875
876         if (status & FE_HAS_LOCK)    /* everything's working... */
877         {
878             int32_t value;
879             msg_Dbg(p_input, "check frontend ... has lock");
880             msg_Dbg(p_input, "tuning succeeded");
881
882             /* Read some statistics */
883             value = 0;
884             if ( ioctl( p_frontend->i_handle, FE_READ_BER, &value ) >= 0 )
885                 msg_Dbg( p_input, "Bit error rate: %d", value );
886
887             value = 0;
888             if ( ioctl( p_frontend->i_handle, FE_READ_SIGNAL_STRENGTH, &value ) >= 0 )
889                 msg_Dbg( p_input, "Signal strength: %d", value );
890
891             value = 0;
892             if ( ioctl( p_frontend->i_handle, FE_READ_SNR, &value ) >= 0 )
893                 msg_Dbg( p_input, "SNR: %d", value );
894
895             return 0;
896         }
897
898         if (status & FE_TIMEDOUT)    /*  no lock within the last ~2 seconds */
899         {
900             msg_Err(p_input, "tuning failed ... timed out");
901             return -2;
902         }
903
904         if (status & FE_REINIT)
905         {
906             /*  frontend was reinitialized,  */
907             /*  application is recommended to reset */
908             /*  DiSEqC, tone and parameters */
909             msg_Err(p_input, "tuning failed ... resend frontend parameters");
910             return -3;
911         }
912
913         msleep(500000);
914     }
915     return -1;
916 }
917
918
919 /*
920  * Demux
921  */
922
923 /*****************************************************************************
924  * DMXSetFilter : controls the demux to add a filter
925  *****************************************************************************/
926 int E_(DMXSetFilter)( input_thread_t * p_input, int i_pid, int * pi_fd,
927                       int i_type )
928 {
929     struct dmx_pes_filter_params s_filter_params;
930     int i_ret;
931     unsigned int i_adapter, i_device;
932     char dmx[128];
933     vlc_value_t val;
934
935     var_Get( p_input, "dvb-adapter", &val );
936     i_adapter = val.i_int;
937     var_Get( p_input, "dvb-device", &val );
938     i_device = val.i_int;
939
940     if ( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
941             >= (int)sizeof(dmx) )
942     {
943         msg_Err( p_input, "snprintf() truncated string for DMX" );
944         dmx[sizeof(dmx) - 1] = '\0';
945     }
946
947     msg_Dbg( p_input, "Opening device %s", dmx );
948     if ( (*pi_fd = open(dmx, O_RDWR)) < 0 )
949     {
950         msg_Err( p_input, "DMXSetFilter: opening device failed (%s)",
951                  strerror(errno) );
952         return -1;
953     }
954
955     /* We fill the DEMUX structure : */
956     s_filter_params.pid     =   i_pid;
957     s_filter_params.input   =   DMX_IN_FRONTEND;
958     s_filter_params.output  =   DMX_OUT_TS_TAP;
959     s_filter_params.flags   =   DMX_IMMEDIATE_START;
960
961     switch ( i_type )
962     {   /* First device */
963         case 1:
964             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
965             s_filter_params.pes_type = DMX_PES_VIDEO0;
966             break;
967         case 2:
968             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
969             s_filter_params.pes_type = DMX_PES_AUDIO0;
970             break;
971         case 3:
972             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
973             s_filter_params.pes_type = DMX_PES_TELETEXT0;
974             break;
975         case 4:
976             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
977             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
978             break;
979         case 5:
980             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
981             s_filter_params.pes_type = DMX_PES_PCR0;
982             break;
983         /* Second device */
984         case 6:
985             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
986             s_filter_params.pes_type = DMX_PES_VIDEO1;
987             break;
988         case 7:
989             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
990             s_filter_params.pes_type = DMX_PES_AUDIO1;
991             break;
992         case 8:
993             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
994             s_filter_params.pes_type = DMX_PES_TELETEXT1;
995             break;
996         case 9:
997             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
998             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
999             break;
1000         case 10:
1001             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1002             s_filter_params.pes_type = DMX_PES_PCR1;
1003             break;
1004         /* Third device */
1005         case 11:
1006             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1007             s_filter_params.pes_type = DMX_PES_VIDEO2;
1008             break;
1009         case 12:
1010             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1011             s_filter_params.pes_type = DMX_PES_AUDIO2;
1012             break;
1013         case 13:
1014             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1015             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1016             break;
1017         case 14:
1018             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1019             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1020             break;
1021         case 15:
1022             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1023             s_filter_params.pes_type = DMX_PES_PCR2;
1024             break;
1025         /* Forth device */
1026         case 16:
1027             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1028             s_filter_params.pes_type = DMX_PES_VIDEO3;
1029             break;
1030         case 17:
1031             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1032             s_filter_params.pes_type = DMX_PES_AUDIO3;
1033             break;
1034         case 18:
1035             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1036             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1037             break;
1038         case 19:
1039             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1040             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1041             break;
1042         case 20:
1043             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1044             s_filter_params.pes_type = DMX_PES_PCR3;
1045             break;
1046         /* Usually used by Nova cards */
1047         case 21:
1048         default:
1049             msg_Dbg(p_input, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1050             s_filter_params.pes_type = DMX_PES_OTHER;
1051             break;
1052     }
1053
1054     /* We then give the order to the device : */
1055     if ( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1056     {
1057         msg_Err( p_input, "DMXSetFilter: failed with %d (%s)", i_ret,
1058                  strerror(errno) );
1059         return -1;
1060     }
1061     return 0;
1062 }
1063
1064 /*****************************************************************************
1065  * DMXUnsetFilter : removes a filter
1066  *****************************************************************************/
1067 int E_(DMXUnsetFilter)( input_thread_t * p_input, int i_fd )
1068 {
1069     int i_ret;
1070
1071     if ( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1072     {
1073         msg_Err( p_input, "DMX_STOP failed for demux (%d) %s",
1074                  i_ret, strerror(errno) );
1075         return i_ret;
1076     }
1077
1078     msg_Dbg( p_input, "DMXUnsetFilter: closing demux %d", i_fd);
1079     close( i_fd );
1080     return 0;
1081 }
1082
1083
1084 /*
1085  * DVR device
1086  */
1087
1088 /*****************************************************************************
1089  * DVROpen :
1090  *****************************************************************************/
1091 int E_(DVROpen)( input_thread_t * p_input )
1092 {
1093     thread_dvb_data_t * p_dvb
1094                 = (thread_dvb_data_t *)p_input->p_access_data;
1095     unsigned int i_adapter, i_device;
1096     char dvr[128];
1097     vlc_value_t val;
1098
1099     var_Get( p_input, "dvb-adapter", &val );
1100     i_adapter = val.i_int;
1101     var_Get( p_input, "dvb-device", &val );
1102     i_device = val.i_int;
1103
1104     if ( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1105             >= (int)sizeof(dvr) )
1106     {
1107         msg_Err( p_input, "snprintf() truncated string for DVR" );
1108         dvr[sizeof(dvr) - 1] = '\0';
1109     }
1110
1111     msg_Dbg( p_input, "Opening device %s", dvr );
1112     if ( (p_dvb->i_handle = open(dvr, O_RDONLY)) < 0 )
1113     {
1114         msg_Err( p_input, "DVROpen: opening device failed (%s)",
1115                  strerror(errno) );
1116         return -1;
1117     }
1118
1119     return 0;
1120 }
1121
1122 /*****************************************************************************
1123  * DVRClose :
1124  *****************************************************************************/
1125 void E_(DVRClose)( input_thread_t * p_input )
1126 {
1127     thread_dvb_data_t * p_dvb
1128                 = (thread_dvb_data_t *)p_input->p_access_data;
1129
1130     close( p_dvb->i_handle );
1131 }
1132