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