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