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