]> git.sesse.net Git - vlc/blob - modules/access/dvb/dvb.c
- Enabled tuning for DVB-C and DVB-T cards.
[vlc] / modules / access / dvb / dvb.c
1 /*****************************************************************************
2  * dvb.c : functions to control a DVB card under Linux with v4l2
3  *****************************************************************************
4  * Copyright (C) 1998-2003 VideoLAN
5  *
6  * Authors: Damien Lucas <nitrox@via.ecp.fr>
7  *          Johan Bilien <jobi@via.ecp.fr>
8  *          Jean-Paul Saman <saman@natlab.research.philips.com>
9  *          Christopher Ross <ross@natlab.research.philips.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    02111, USA.
24  *****************************************************************************/
25
26 #include <vlc/vlc.h>
27 #include <vlc/input.h>
28
29 #include <sys/ioctl.h>
30 #include <stdio.h>
31 #ifdef HAVE_ERRNO_H
32 #    include <string.h>
33 #    include <errno.h>
34 #endif
35
36 #ifdef HAVE_INTTYPES_H
37 #   include <inttypes.h>                                       /* int16_t .. */
38 #endif
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/poll.h>
47
48 /* DVB Card Drivers */
49 #include <linux/dvb/dmx.h>
50 #include <linux/dvb/frontend.h>
51
52 #include <linux/errno.h>
53
54 #include "dvb.h"
55
56 struct diseqc_cmd_t
57 {
58     struct dvb_diseqc_master_cmd cmd;
59     uint32_t wait;
60 };
61
62 struct diseqc_cmd_t switch_cmds[] =
63 {
64     { { { 0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00 }, 4 }, 0 },
65     { { { 0xe0, 0x10, 0x38, 0xf2, 0x00, 0x00 }, 4 }, 0 },
66     { { { 0xe0, 0x10, 0x38, 0xf1, 0x00, 0x00 }, 4 }, 0 },
67     { { { 0xe0, 0x10, 0x38, 0xf3, 0x00, 0x00 }, 4 }, 0 },
68     { { { 0xe0, 0x10, 0x38, 0xf4, 0x00, 0x00 }, 4 }, 0 },
69     { { { 0xe0, 0x10, 0x38, 0xf6, 0x00, 0x00 }, 4 }, 0 },
70     { { { 0xe0, 0x10, 0x38, 0xf5, 0x00, 0x00 }, 4 }, 0 },
71     { { { 0xe0, 0x10, 0x38, 0xf7, 0x00, 0x00 }, 4 }, 0 },
72     { { { 0xe0, 0x10, 0x38, 0xf8, 0x00, 0x00 }, 4 }, 0 },
73     { { { 0xe0, 0x10, 0x38, 0xfa, 0x00, 0x00 }, 4 }, 0 },
74     { { { 0xe0, 0x10, 0x38, 0xf9, 0x00, 0x00 }, 4 }, 0 },
75     { { { 0xe0, 0x10, 0x38, 0xfb, 0x00, 0x00 }, 4 }, 0 },
76     { { { 0xe0, 0x10, 0x38, 0xfc, 0x00, 0x00 }, 4 }, 0 },
77     { { { 0xe0, 0x10, 0x38, 0xfe, 0x00, 0x00 }, 4 }, 0 },
78     { { { 0xe0, 0x10, 0x38, 0xfd, 0x00, 0x00 }, 4 }, 0 },
79     { { { 0xe0, 0x10, 0x38, 0xff, 0x00, 0x00 }, 4 }, 0 }
80 };
81
82 static int ioctl_CheckFrontend(input_thread_t * p_input, int front);
83
84 /*****************************************************************************
85  * ioctl_FrontendControl : commands the SEC device
86  *****************************************************************************/
87 int ioctl_FrontendControl(input_thread_t * p_input, int freq, int pol, int lnb_slof,
88                           int diseqc, unsigned int u_adapter, unsigned int u_device)
89 {
90     struct dvb_diseqc_master_cmd  cmd;
91     fe_sec_tone_mode_t tone;
92     fe_sec_voltage_t voltage;
93     int frontend;
94     char front[] = FRONTEND;
95     int i_len;
96
97     i_len = sizeof(FRONTEND);
98     if (snprintf(front, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
99     {
100         msg_Err(p_input, "snprintf() truncated string for FRONTEND" );
101         front[sizeof(FRONTEND)] = '\0';
102     }
103
104     msg_Dbg(p_input, "Opening frontend %s",front);        
105     if((frontend = open(front,O_RDWR)) < 0)
106     {
107 #   ifdef HAVE_ERRNO_H
108         msg_Err(p_input, "ioctl_FrontEndControl: Opening frontend failed (%s)",strerror(errno));
109 #   else
110         msg_Err(p_input, "ioctl_FrontEndControl: Opening frontend failed");
111 #   endif
112         return -1;
113     }
114
115     /* Set the frequency of the transponder, taking into account the
116        local frequencies of the LNB */
117     tone = (freq<lnb_slof) ? SEC_TONE_OFF : SEC_TONE_ON;
118
119     /* Set the polarisation of the transponder by setting the correct
120        voltage on the universal LNB */
121     voltage = (pol) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13;
122     
123     /* In case we have a DiSEqC, set it to the correct address */
124     cmd.msg[0] = 0x0;  /* framing */
125     cmd.msg[1] = 0x10; /* address */
126     cmd.msg[2] = 0x38; /* command */
127     /* command parameters start at index 3 */
128     cmd.msg[3] = 0xF0 | ((diseqc * 4) & 0x0F);   
129     cmd.msg_len = 4;
130
131     /* Reset everything before sending. */
132 #ifdef HAVE_ERRNO_H 
133 #   define CHECK_IOCTL(X) if(X<0) \
134     { \
135         msg_Err( p_input, "InfoFrontend: ioctl failed (%s)", strerror(errno)); \
136         close(frontend); \
137         return -1; \
138     }
139 #else
140 #   define CHECK_IOCTL(X) if(X<0) \
141     { \
142         msg_Err( p_input, "InfoFrontend: ioctl failed"); \
143         close(frontend); \
144         return -1; \
145     }
146 #endif
147
148     CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, SEC_TONE_OFF));   
149     CHECK_IOCTL(ioctl(frontend, FE_SET_VOLTAGE, voltage));
150     msleep(15);
151     
152     /* Send the data to the SEC device to prepare the LNB for tuning  */
153     CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd));
154     msleep(15);
155     CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_BURST, &cmd));
156     msleep(15);
157     CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, tone));
158 #undef CHECK_IOCTL
159
160     close(frontend);
161     return 0;
162 }
163
164 /*****************************************************************************
165  * ioctl_InfoFrontend : return information about given frontend
166  *****************************************************************************/
167 int ioctl_InfoFrontend(input_thread_t * p_input, struct dvb_frontend_info *info,
168                        unsigned int u_adapter, unsigned int u_device)
169 {
170     int front;
171     int ret;
172     char frontend[] = FRONTEND;
173     int i_len;
174
175     i_len = sizeof(FRONTEND);
176     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
177     {
178         msg_Err(p_input, "snprintf() truncated string for FRONTEND" );
179         frontend[sizeof(FRONTEND)] = '\0';
180     }
181
182     msg_Dbg(p_input, "Opening device %s", frontend);
183     if((front = open(frontend,O_RDWR)) < 0)
184     {
185 #   ifdef HAVE_ERRNO_H
186         msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed (%s)", strerror(errno));
187 #   else
188         msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed");
189 #   endif
190       return -1;
191     }
192
193     /* Determine type of frontend */
194     if ((ret=ioctl(front, FE_GET_INFO, info)) < 0)
195     {
196         close(front);
197 #   ifdef HAVE_ERRNO_H
198         msg_Err(p_input, "ioctl FE_GET_INFO failed (%d) %s", ret, strerror(errno));
199 #   else
200         msg_Err(p_input, "ioctl FE_GET_INFO failed (%d)", ret);
201 #   endif
202         return -1;
203     }
204     /* Print out frontend capabilities. */
205     msg_Dbg(p_input, "Frontend Info:\tname = %s\n\t\tfrequency_min = %d\n\t\tfrequency_max = %d\n\t\tfrequency_stepsize = %d\n\t\tfrequency_tolerance = %d\n\t\tsymbol_rate_min = %d\n\t\tsymbol_rate_max = %d\n\t\tsymbol_rate_tolerance (ppm) = %d\n\t\tnotifier_delay (ms)= %d\n",
206             info->name,
207             info->frequency_min,
208             info->frequency_max,
209             info->frequency_stepsize,
210             info->frequency_tolerance,
211             info->symbol_rate_min,
212             info->symbol_rate_max,
213             info->symbol_rate_tolerance,
214             info->notifier_delay );
215     msg_Dbg(p_input, "Frontend Info capability list:");
216     if (info->caps&FE_IS_STUPID)
217         msg_Dbg(p_input, "no capabilities - frontend is stupid!");
218     if (info->caps&FE_CAN_INVERSION_AUTO)
219         msg_Dbg(p_input, "inversion auto");
220     if (info->caps&FE_CAN_FEC_1_2)
221         msg_Dbg(p_input, "forward error correction 1/2");
222     if (info->caps&FE_CAN_FEC_2_3)
223         msg_Dbg(p_input, "forward error correction 2/3");
224     if (info->caps&FE_CAN_FEC_3_4)
225         msg_Dbg(p_input, "forward error correction 3/4");
226     if (info->caps&FE_CAN_FEC_4_5)
227         msg_Dbg(p_input, "forward error correction 4/5");
228     if (info->caps&FE_CAN_FEC_5_6)
229         msg_Dbg(p_input, "forward error correction 5/6");
230     if (info->caps&FE_CAN_FEC_6_7)
231         msg_Dbg(p_input, "forward error correction 6/7");
232     if (info->caps&FE_CAN_FEC_7_8)
233         msg_Dbg(p_input, "forward error correction 7/8");
234     if (info->caps&FE_CAN_FEC_8_9)
235         msg_Dbg(p_input, "forward error correction 8/9");
236     if (info->caps&FE_CAN_FEC_AUTO)
237         msg_Dbg(p_input, "forward error correction auto");
238     if (info->caps&FE_CAN_QPSK)
239         msg_Dbg(p_input, "card can do QPSK");
240     if (info->caps&FE_CAN_QAM_16)
241         msg_Dbg(p_input, "card can do QAM 16");
242     if (info->caps&FE_CAN_QAM_32)
243         msg_Dbg(p_input, "card can do QAM 32");
244     if (info->caps&FE_CAN_QAM_64)
245         msg_Dbg(p_input, "card can do QAM 64");
246     if (info->caps&FE_CAN_QAM_128)
247         msg_Dbg(p_input, "card can do QAM 128");
248     if (info->caps&FE_CAN_QAM_256)
249         msg_Dbg(p_input, "card can do QAM 256");
250     if (info->caps&FE_CAN_QAM_AUTO)
251         msg_Dbg(p_input, "card can do QAM auto");
252     if (info->caps&FE_CAN_TRANSMISSION_MODE_AUTO)
253         msg_Dbg(p_input, "transmission mode auto");
254     if (info->caps&FE_CAN_BANDWIDTH_AUTO)
255         msg_Dbg(p_input, "bandwidth mode auto");
256     if (info->caps&FE_CAN_GUARD_INTERVAL_AUTO)
257         msg_Dbg(p_input, "guard interval mode auto");
258     if (info->caps&FE_CAN_HIERARCHY_AUTO)
259         msg_Dbg(p_input, "hierarchy mode auto");
260     if (info->caps&FE_CAN_MUTE_TS)
261         msg_Dbg(p_input, "card can mute TS");
262     if (info->caps&FE_CAN_CLEAN_SETUP)
263         msg_Dbg(p_input, "clean setup");        
264     msg_Dbg(p_input,"End of capability list");
265     
266     close(front);
267     return 0;
268 }
269
270 int ioctl_DiseqcSendMsg (input_thread_t *p_input, int fd, fe_sec_voltage_t v, struct diseqc_cmd_t **cmd,
271                          fe_sec_tone_mode_t t, fe_sec_mini_cmd_t b)
272 {
273     int err;
274
275     if ((err = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF))<0)
276     {
277 #   ifdef HAVE_ERRNO_H
278         msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d) %s", SEC_TONE_ON ? "on" : "off", err, strerror(errno));
279 #   else
280         msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d)", SEC_TONE_ON ? "on" : "off", err);
281 #   endif
282         return err;
283     }
284     if ((err = ioctl(fd, FE_SET_VOLTAGE, v))<0)
285     {
286 #   ifdef HAVE_ERRNO_H
287         msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d) %s", v, err, strerror(errno));
288 #   else
289         msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d)", v, err);
290 #   endif
291         return err;
292     }
293
294     msleep(15);
295     while (*cmd)
296     {
297         msg_Dbg(p_input, "DiseqcSendMsg(): %02x %02x %02x %02x %02x %02x",
298             (*cmd)->cmd.msg[0], (*cmd)->cmd.msg[1],
299             (*cmd)->cmd.msg[2], (*cmd)->cmd.msg[3],
300             (*cmd)->cmd.msg[4], (*cmd)->cmd.msg[5]);
301
302         if ((err = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &(*cmd)->cmd))<0)
303         {
304 #       ifdef HAVE_ERRNO_H
305             msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d) %s", err, strerror(errno));
306 #       else
307             msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d)", err);
308 #       endif
309             return err;
310         }
311
312         msleep((*cmd)->wait);
313         cmd++;
314     }
315
316     msleep(15);
317
318     if ((err = ioctl(fd, FE_DISEQC_SEND_BURST, b))<0)
319     {
320 #   ifdef HAVE_ERRNO_H
321         msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d) %s",b, err, strerror(errno));
322 #   else
323         msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d)",b, err);
324 #   endif
325       return err;
326     }
327     msleep(15);
328
329     if ((err = ioctl(fd, FE_SET_TONE, t))<0)
330     {
331 #   ifdef HAVE_ERRNO_H
332         msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d) %s", t, err, strerror(errno));
333 #   else
334         msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d)", t, err);
335 #   endif
336         return err;
337     }
338     return err; 
339 }
340
341 int ioctl_SetupSwitch (input_thread_t *p_input, int frontend_fd, int switch_pos,
342                        int voltage_18, int hiband)
343 {
344     int ret;
345     struct diseqc_cmd_t *cmd[2] = { NULL, NULL };
346     int i = 4 * switch_pos + 2 * hiband + (voltage_18 ? 1 : 0);
347
348     msg_Dbg(p_input, "ioctl_SetupSwitch: switch pos %i, %sV, %sband",
349             switch_pos, voltage_18 ? "18" : "13", hiband ? "hi" : "lo");
350     msg_Dbg(p_input, "ioctl_SetupSwitch: index %i", i);
351
352     if ((i < 0) || (i >= (int)(sizeof(switch_cmds)/sizeof(struct diseqc_cmd_t))))
353         return -EINVAL;
354
355     cmd[0] = &switch_cmds[i];
356
357     if ((ret = ioctl_DiseqcSendMsg (p_input, frontend_fd,
358           (i % 2) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13,
359           cmd,
360           (i/2) % 2 ? SEC_TONE_ON : SEC_TONE_OFF,
361           (i/4) % 2 ? SEC_MINI_B : SEC_MINI_A))<0)
362     {
363         msg_Err(p_input, "ioctl_DiseqcSendMsg() failed (%d)", ret);
364         return ret;
365     }
366
367     return ret;
368 }
369
370 #define SWITCHFREQ 11700000
371 #define LOF_HI     10600000
372 #define LOF_LO      9750000
373
374 /*****************************************************************************
375  * ioctl_SetFrontend : controls the FE device
376  *****************************************************************************/
377 int ioctl_SetFrontend (input_thread_t * p_input, struct dvb_frontend_parameters fep,
378                        int b_polarisation, unsigned int u_adapter, unsigned int u_device  )
379 {
380     int front;
381     int ret;
382     int i;
383     int hiband;
384     char frontend[] = FRONTEND;
385     int i_len;
386
387     i_len = sizeof(FRONTEND);
388     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
389     {
390         msg_Err(p_input,  "ioctl_SetFrontEnd snprintf() truncated string for FRONTEND" );
391         frontend[sizeof(FRONTEND)] = '\0';
392     }
393     
394     /* Open the frontend device */
395           msg_Dbg(p_input, "Opening frontend %s", frontend);
396     if((front = open(frontend,O_RDWR)) < 0)
397     {
398 #   ifdef HAVE_ERRNO_H
399         msg_Err(p_input, "failed to open frontend (%s)", strerror(errno));
400 #   else
401         msg_Err(p_input, "failed to open frontend");
402 #   endif
403         return -1;
404     }
405     
406     /* Set the frequency of the transponder, taking into account the
407        local frequencies of the LNB */
408     hiband = (fep.frequency >= SWITCHFREQ);
409     if ((ret=ioctl_SetupSwitch (p_input, front, 0, b_polarisation, hiband))<0)
410     {
411         msg_Err(p_input, "ioctl_SetupSwitch failed (%d)", ret);
412         return -1;
413     }
414
415     if (hiband)
416         fep.frequency -= LOF_HI;
417     else
418         fep.frequency -= LOF_LO;
419
420     /* Now send it all to the frontend device */
421     if ((ret=ioctl(front, FE_SET_FRONTEND, &fep)) < 0)
422     {
423         close(front);
424 #   ifdef HAVE_ERRNO_H
425         msg_Err(p_input, "ioctl_SetFrontend: ioctl FE_SET_FRONTEND failed (%d) %s", ret, strerror(errno));
426 #   else
427         msg_Err(p_input, "ioctl_SetFrontend: ioctl FE_SET_FRONTEND failed (%d)", ret);
428 #   endif
429         return -1;
430     }
431
432     for (i=0; i<3; i++)
433     {
434         fe_status_t s;
435         if ((ret=ioctl(front, FE_READ_STATUS, &s))<0)
436         {
437 #       ifdef HAVE_ERRNO_H
438             msg_Err(p_input, "ioctl FE_READ_STATUS failed (%d) %s", ret, strerror(errno));
439 #       else
440             msg_Err(p_input, "ioctl FE_READ_STATUS failed (%d)", ret);
441 #       endif
442         }
443
444         if (s & FE_HAS_LOCK)
445         {
446             msg_Dbg(p_input, "ioctl_SetFrontend: tuning status == 0x%02x!!! ..."
447                              "tuning succeeded", s);
448             ret = 0;
449         }
450         else
451         {
452             msg_Dbg(p_input, "ioctl_SetFrontend: tuning status == 0x%02x!!! ..."
453                              "tuning failed", s);
454             ret = -1;
455         }
456         usleep( 500000 );
457     }
458
459     /* Close front end device */
460     close(front);
461     return ret;
462 }
463
464 /******************************************************************
465  * Check completion of the frontend control sequence
466  ******************************************************************/
467 static int ioctl_CheckFrontend(input_thread_t * p_input, int front)
468 {
469     int ret;
470     struct pollfd pfd[1];
471     struct dvb_frontend_event event;
472     /* poll for frontend event to check if tuning worked */
473     pfd[0].fd = front;
474     pfd[0].events = POLLIN;
475
476     if (poll(pfd,1,3000))
477     {
478         if (pfd[0].revents & POLLIN)
479         {
480             if ( (ret=ioctl(front, FE_GET_EVENT, &event)) < 0)
481             {
482 #           ifdef HAVE_ERRNO_H
483                 msg_Err(p_input, "ioctl_CheckFrontend: ioctl FE_GET_EVENT failed (%d) %s", ret, strerror(errno));
484 #           else
485                 msg_Err(p_input, "ioctl_CheckFrontend: ioctl FE_GET_EVENT failed (%d)", ret);
486 #           endif
487                 return -5;
488             }
489
490             switch(event.status)
491             {
492                 case FE_HAS_SIGNAL:  /* found something above the noise level */
493                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_HAS_SIGNAL");
494                     break;
495                 case FE_HAS_CARRIER: /* found a DVB signal  */
496                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_HAS_CARRIER");
497                     break;
498                 case FE_HAS_VITERBI: /* FEC is stable  */
499                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_HAS_VITERBI");
500                     break;
501                 case FE_HAS_SYNC:    /* found sync bytes  */
502                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_HAS_SYNC");
503                     break;
504                 case FE_HAS_LOCK:    /* everything's working... */
505                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_HAS_LOCK");
506                     break;
507                 case FE_TIMEDOUT:    /*  no lock within the last ~2 seconds */
508                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_TIMEDOUT");
509                     return -2;
510                 case FE_REINIT:      /*  frontend was reinitialized,  */
511                                      /*  application is recommned to reset */
512                                      /*  DiSEqC, tone and parameters */
513                     msg_Dbg(p_input, "ioctl_CheckFrontend: FE_REINIT");
514                     return -1;
515             }
516         }
517         else
518         {
519             /* should come here */
520             msg_Err(p_input, "ioctl_CheckFrontend: event() failed");
521             return -3;
522         }
523     }
524     else
525     {
526 #   ifdef HAVE_ERRNO_H
527         msg_Err(p_input, "ioctl_CheckFrontend: poll() failed (%s)", strerror(errno));
528 #   else
529         msg_Err(p_input, "ioctl_CheckFrontend: poll() failed");
530 #   endif
531         return -4;
532     }
533
534     return 0;
535 }
536
537 /*****************************************************************************
538  * ioctl_SetDMXFilter : controls the demux to add a filter
539  *****************************************************************************/
540 int ioctl_SetDMXFilter(input_thread_t * p_input, int i_pid, int * pi_fd , int i_type,
541                        unsigned int u_adapter, unsigned int u_device )
542 {
543     struct dmx_pes_filter_params s_filter_params;
544     char dmx[] = DMX;
545     int i_len;
546     int result;
547
548     /* We first open the device */
549     i_len = sizeof(DMX);
550     if (snprintf( dmx, sizeof(DMX), DMX, u_adapter, u_device) >= i_len)
551     {
552         msg_Err(p_input,  "snprintf() truncated string for DMX" );
553         dmx[sizeof(DMX)] = '\0';
554     }
555
556     msg_Dbg(p_input, "Opening demux device %s", dmx);
557     if ((*pi_fd = open(dmx, O_RDWR|O_NONBLOCK))  < 0)
558     {
559 #   ifdef HAVE_ERRNO_H
560         msg_Err(p_input, "ioctl_SetDMXFilter: opening device failed (%s)", strerror(errno));
561 #   else
562         msg_Err(p_input, "ioctl_SetDMXFilter: opening device failed");
563 #   endif
564         return -1;
565     }
566
567     /* We fill the DEMUX structure : */
568     s_filter_params.pid     =   i_pid;
569     s_filter_params.input   =   DMX_IN_FRONTEND;
570     s_filter_params.output  =   DMX_OUT_TS_TAP;
571     switch ( i_type )
572     {
573         case 1:
574             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO for PMT %d", i_pid);
575             s_filter_params.pes_type = DMX_PES_VIDEO;
576             break;
577         case 2:
578             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO for PMT %d", i_pid);
579             s_filter_params.pes_type = DMX_PES_AUDIO;
580             break;
581         case 3:
582             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_OTHER for PMT %d", i_pid);
583             s_filter_params.pes_type = DMX_PES_OTHER;
584             break;
585         case 4:
586             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO1 for PMT %d", i_pid);
587             s_filter_params.pes_type = DMX_PES_VIDEO1;
588             break;
589         case 5:
590             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO1 for PMT %d", i_pid);
591             s_filter_params.pes_type = DMX_PES_AUDIO1;
592             break;
593
594         case 7:
595             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO2 for PMT %d", i_pid);
596             s_filter_params.pes_type = DMX_PES_VIDEO2;
597             break;
598         case 8:
599             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO2 for PMT %d", i_pid);
600             s_filter_params.pes_type = DMX_PES_AUDIO2;
601             break;
602             
603         case 10:
604             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO3 for PMT %d", i_pid);
605             s_filter_params.pes_type = DMX_PES_VIDEO3;
606             break;
607         case 11:
608             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO3 for PMT %d", i_pid);
609             s_filter_params.pes_type = DMX_PES_AUDIO3;
610             break;
611         default:
612             msg_Err(p_input, "trying to set PMT id to=%d for unknown type %d", i_pid, i_type );
613             break;
614     }
615     s_filter_params.flags = DMX_IMMEDIATE_START;
616
617     /* We then give the order to the device : */
618     if ((result = ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params)) < 0)
619     {
620 #   ifdef HAVE_ERRNO_H
621         msg_Err(p_input, "ioctl_SetDMXFilter: ioctl failed with %d (%s)",result, strerror(errno));
622 #   else
623         msg_Err(p_input, "ioctl_SetDMXFilter: ioctl failed with %d",result);
624 #   endif
625         return -1;
626     }
627     return 0;
628 }
629
630 /*****************************************************************************
631  * ioctl_UnsetDMXFilter : removes a filter
632  *****************************************************************************/
633 int ioctl_UnsetDMXFilter(input_thread_t * p_input, int demux)
634 {
635     int ret;
636     
637     if ((ret=ioctl(demux, DMX_STOP))<0)
638     {
639 #   ifdef HAVE_ERRNO_H
640         msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d) %s", demux, ret, strerror(errno));
641 #   else
642         msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d)", demux, ret);
643 #   endif
644         return -1;
645     }
646     close(demux);
647     return 0;
648 }
649
650 /*****************************************************************************
651  * dvb_DecodeBandwidth : decodes arguments for DVB S/C/T card
652  *****************************************************************************/
653 fe_bandwidth_t dvb_DecodeBandwidth(input_thread_t * p_input, int bandwidth)
654 {
655     fe_bandwidth_t      fe_bandwidth = 0;
656     
657     switch (bandwidth)
658     {
659         case 0:
660             fe_bandwidth = BANDWIDTH_AUTO;
661             break;
662         case 6:
663             fe_bandwidth = BANDWIDTH_6_MHZ;
664             break;
665         case 7:
666             fe_bandwidth = BANDWIDTH_7_MHZ;
667             break;
668         case 8:
669             fe_bandwidth = BANDWIDTH_8_MHZ;
670             break;
671         default:
672             msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto");
673             fe_bandwidth = BANDWIDTH_AUTO;
674             break;
675     }
676     
677     return fe_bandwidth;
678 }
679
680 fe_code_rate_t dvb_DecodeFEC(input_thread_t * p_input, int fec)
681 {
682     fe_code_rate_t fe_fec = FEC_NONE;
683     
684     switch( fec )
685     {
686         case 1:
687             fe_fec = FEC_1_2;
688             break;
689         case 2:
690             fe_fec = FEC_2_3;
691             break;
692         case 3:
693             fe_fec = FEC_3_4;
694             break;
695         case 4:
696             fe_fec = FEC_4_5;
697             break;
698         case 5:
699             fe_fec = FEC_5_6;
700             break;
701         case 6:
702             fe_fec = FEC_6_7;
703             break;
704         case 7:
705             fe_fec = FEC_7_8;
706             break;
707         case 8:
708             fe_fec = FEC_8_9;
709             break;
710         case 9:
711             fe_fec = FEC_AUTO;
712             break;
713         default:
714             /* cannot happen */
715             fe_fec = FEC_NONE;
716             msg_Err( p_input, "argument has invalid FEC (%d)", fec);
717             break;
718     }    
719     return fe_fec;
720 }
721
722 fe_modulation_t dvb_DecodeModulation(input_thread_t * p_input, int modulation)
723 {
724     fe_modulation_t     fe_modulation = 0;
725     
726     switch( modulation )
727     {
728         case -1:
729             fe_modulation = QPSK;
730             break;
731         case 0:
732             fe_modulation = QAM_AUTO;
733             break;
734         case 16:
735             fe_modulation = QAM_16;
736             break;
737         case 32:
738             fe_modulation = QAM_32;
739             break;
740         case 64:
741             fe_modulation = QAM_64;
742             break;
743         case 128:
744             fe_modulation = QAM_128;
745             break;
746         case 256:
747             fe_modulation = QAM_256;
748             break;
749         default:
750             msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
751             fe_modulation = QAM_AUTO;
752             break;
753     }    
754     return fe_modulation;
755 }
756
757 fe_transmit_mode_t dvb_DecodeTransmission(input_thread_t * p_input, int transmission)
758 {
759     fe_transmit_mode_t  fe_transmission = 0;
760     
761     switch( transmission )
762     {
763         case 0:
764             fe_transmission = TRANSMISSION_MODE_AUTO;
765             break;
766         case 2:
767             fe_transmission = TRANSMISSION_MODE_2K;
768             break;
769         case 8:
770             fe_transmission = TRANSMISSION_MODE_8K;
771             break;
772         default:
773             msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
774             fe_transmission = TRANSMISSION_MODE_AUTO;
775             break;
776     }    
777     return fe_transmission;
778 }
779
780 fe_guard_interval_t dvb_DecodeGuardInterval(input_thread_t * p_input, int guard)
781 {
782     fe_guard_interval_t fe_guard = 0;
783
784     switch( guard )
785     {
786         case 0:
787             fe_guard = GUARD_INTERVAL_AUTO;
788             break;
789         case 4:
790             fe_guard = GUARD_INTERVAL_1_4;
791             break;
792         case 8:
793             fe_guard = GUARD_INTERVAL_1_8;
794             break;
795         case 16:
796             fe_guard = GUARD_INTERVAL_1_16;
797             break;
798         case 32:
799             fe_guard = GUARD_INTERVAL_1_32;
800             break;
801         default:
802             msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
803             fe_guard = GUARD_INTERVAL_AUTO;
804             break;
805     }
806     return fe_guard;
807 }
808
809 fe_hierarchy_t dvb_DecodeHierarchy(input_thread_t * p_input, int hierarchy)
810 {
811     fe_hierarchy_t      fe_hierarchy = 0;
812
813     switch (hierarchy)
814     {
815         case -1:
816             fe_hierarchy = HIERARCHY_NONE;
817             break;
818         case 0:
819             fe_hierarchy = HIERARCHY_AUTO;
820             break;
821         case 1:
822             fe_hierarchy = HIERARCHY_1;
823             break;
824         case 2:
825             fe_hierarchy = HIERARCHY_2;
826             break;
827         case 4:
828             fe_hierarchy = HIERARCHY_4;
829             break;
830         default:
831             msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
832             fe_hierarchy = HIERARCHY_AUTO;
833             break;
834     }
835     return fe_hierarchy;
836 }
837
838 fe_spectral_inversion_t dvb_DecodeInversion(input_thread_t * p_input, int inversion)
839 {
840     fe_spectral_inversion_t fe_inversion=0;
841
842     switch (inversion)
843     {
844         case 0:
845             fe_inversion = INVERSION_OFF;
846             break;
847         case 1:
848             fe_inversion = INVERSION_ON;
849             break;
850         case 2:
851             fe_inversion = INVERSION_AUTO;
852             break;
853         default:
854             msg_Dbg( p_input, "dvb has inversion/polarisation not set, using auto");
855             fe_inversion = INVERSION_AUTO;
856             break;
857     }
858     return fe_inversion;
859