]> git.sesse.net Git - vlc/blob - modules/access/dvb/dvb.c
Rework in DVB plugin to allow tuning to 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 <jpsaman@wxs.nl>
9  *          Christopher Ross <chris@tebibyte.org>
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_InfoFrontend : return information about given frontend
86  *****************************************************************************/
87 int ioctl_InfoFrontend(input_thread_t * p_input, struct dvb_frontend_info *info,
88                        unsigned int u_adapter, unsigned int u_device)
89 {
90     int front;
91     int ret;
92     char frontend[] = FRONTEND;
93     int i_len;
94
95     i_len = sizeof(FRONTEND);
96     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
97     {
98         msg_Err(p_input, "snprintf() truncated string for FRONTEND" );
99         frontend[sizeof(FRONTEND)] = '\0';
100     }
101
102     msg_Dbg(p_input, "Opening device %s", frontend);
103     if((front = open(frontend,O_RDWR)) < 0)
104     {
105 #   ifdef HAVE_ERRNO_H
106         msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed (%s)", strerror(errno));
107 #   else
108         msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed");
109 #   endif
110       return -1;
111     }
112
113     /* Determine type of frontend */
114     if ((ret=ioctl(front, FE_GET_INFO, info)) < 0)
115     {
116         close(front);
117 #   ifdef HAVE_ERRNO_H
118         msg_Err(p_input, "ioctl FE_GET_INFO failed (%d) %s", ret, strerror(errno));
119 #   else
120         msg_Err(p_input, "ioctl FE_GET_INFO failed (%d)", ret);
121 #   endif
122         return -1;
123     }
124     /* Print out frontend capabilities. */
125     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",
126             info->name,
127             info->frequency_min,
128             info->frequency_max,
129             info->frequency_stepsize,
130             info->frequency_tolerance,
131             info->symbol_rate_min,
132             info->symbol_rate_max,
133             info->symbol_rate_tolerance,
134             info->notifier_delay );
135     msg_Dbg(p_input, "Frontend Info capability list:");
136     if (info->caps&FE_IS_STUPID)
137         msg_Dbg(p_input, "no capabilities - frontend is stupid!");
138     if (info->caps&FE_CAN_INVERSION_AUTO)
139         msg_Dbg(p_input, "inversion auto");
140     if (info->caps&FE_CAN_FEC_1_2)
141         msg_Dbg(p_input, "forward error correction 1/2");
142     if (info->caps&FE_CAN_FEC_2_3)
143         msg_Dbg(p_input, "forward error correction 2/3");
144     if (info->caps&FE_CAN_FEC_3_4)
145         msg_Dbg(p_input, "forward error correction 3/4");
146     if (info->caps&FE_CAN_FEC_4_5)
147         msg_Dbg(p_input, "forward error correction 4/5");
148     if (info->caps&FE_CAN_FEC_5_6)
149         msg_Dbg(p_input, "forward error correction 5/6");
150     if (info->caps&FE_CAN_FEC_6_7)
151         msg_Dbg(p_input, "forward error correction 6/7");
152     if (info->caps&FE_CAN_FEC_7_8)
153         msg_Dbg(p_input, "forward error correction 7/8");
154     if (info->caps&FE_CAN_FEC_8_9)
155         msg_Dbg(p_input, "forward error correction 8/9");
156     if (info->caps&FE_CAN_FEC_AUTO)
157         msg_Dbg(p_input, "forward error correction auto");
158     if (info->caps&FE_CAN_QPSK)
159         msg_Dbg(p_input, "card can do QPSK");
160     if (info->caps&FE_CAN_QAM_16)
161         msg_Dbg(p_input, "card can do QAM 16");
162     if (info->caps&FE_CAN_QAM_32)
163         msg_Dbg(p_input, "card can do QAM 32");
164     if (info->caps&FE_CAN_QAM_64)
165         msg_Dbg(p_input, "card can do QAM 64");
166     if (info->caps&FE_CAN_QAM_128)
167         msg_Dbg(p_input, "card can do QAM 128");
168     if (info->caps&FE_CAN_QAM_256)
169         msg_Dbg(p_input, "card can do QAM 256");
170     if (info->caps&FE_CAN_QAM_AUTO)
171         msg_Dbg(p_input, "card can do QAM auto");
172     if (info->caps&FE_CAN_TRANSMISSION_MODE_AUTO)
173         msg_Dbg(p_input, "transmission mode auto");
174     if (info->caps&FE_CAN_BANDWIDTH_AUTO)
175         msg_Dbg(p_input, "bandwidth mode auto");
176     if (info->caps&FE_CAN_GUARD_INTERVAL_AUTO)
177         msg_Dbg(p_input, "guard interval mode auto");
178     if (info->caps&FE_CAN_HIERARCHY_AUTO)
179         msg_Dbg(p_input, "hierarchy mode auto");
180     if (info->caps&FE_CAN_MUTE_TS)
181         msg_Dbg(p_input, "card can mute TS");
182     if (info->caps&FE_CAN_CLEAN_SETUP)
183         msg_Dbg(p_input, "clean setup");        
184     msg_Dbg(p_input,"End of capability list");
185     
186     close(front);
187     return 0;
188 }
189
190 /* QPSK only */
191 int ioctl_DiseqcSendMsg (input_thread_t *p_input, int fd, fe_sec_voltage_t v, struct diseqc_cmd_t **cmd,
192                          fe_sec_tone_mode_t t, fe_sec_mini_cmd_t b)
193 {
194     int err;
195
196     if ((err = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF))<0)
197     {
198 #   ifdef HAVE_ERRNO_H
199         msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d) %s", SEC_TONE_ON ? "on" : "off", err, strerror(errno));
200 #   else
201         msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d)", SEC_TONE_ON ? "on" : "off", err);
202 #   endif
203         return err;
204     }
205     if ((err = ioctl(fd, FE_SET_VOLTAGE, v))<0)
206     {
207 #   ifdef HAVE_ERRNO_H
208         msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d) %s", v, err, strerror(errno));
209 #   else
210         msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d)", v, err);
211 #   endif
212         return err;
213     }
214
215     msleep(15);
216     while (*cmd)
217     {
218         msg_Dbg(p_input, "DiseqcSendMsg(): %02x %02x %02x %02x %02x %02x",
219             (*cmd)->cmd.msg[0], (*cmd)->cmd.msg[1],
220             (*cmd)->cmd.msg[2], (*cmd)->cmd.msg[3],
221             (*cmd)->cmd.msg[4], (*cmd)->cmd.msg[5]);
222
223         if ((err = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &(*cmd)->cmd))<0)
224         {
225 #       ifdef HAVE_ERRNO_H
226             msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d) %s", err, strerror(errno));
227 #       else
228             msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d)", err);
229 #       endif
230             return err;
231         }
232
233         msleep((*cmd)->wait);
234         cmd++;
235     }
236
237     msleep(15);
238
239     if ((err = ioctl(fd, FE_DISEQC_SEND_BURST, b))<0)
240     {
241 #   ifdef HAVE_ERRNO_H
242         msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d) %s",b, err, strerror(errno));
243 #   else
244         msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d)",b, err);
245 #   endif
246       return err;
247     }
248     msleep(15);
249
250     if ((err = ioctl(fd, FE_SET_TONE, t))<0)
251     {
252 #   ifdef HAVE_ERRNO_H
253         msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d) %s", t, err, strerror(errno));
254 #   else
255         msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d)", t, err);
256 #   endif
257         return err;
258     }
259     return err; 
260 }
261
262 /* QPSK only */
263 int ioctl_SetupSwitch (input_thread_t *p_input, int frontend_fd, int switch_pos,
264                        int voltage_18, int hiband)
265 {
266     int ret;
267     struct diseqc_cmd_t *cmd[2] = { NULL, NULL };
268     int i = 4 * switch_pos + 2 * hiband + (voltage_18 ? 1 : 0);
269
270     msg_Dbg(p_input, "ioctl_SetupSwitch: switch pos %i, %sV, %sband",
271             switch_pos, voltage_18 ? "18" : "13", hiband ? "hi" : "lo");
272     msg_Dbg(p_input, "ioctl_SetupSwitch: index %i", i);
273
274     if ((i < 0) || (i >= (int)(sizeof(switch_cmds)/sizeof(struct diseqc_cmd_t))))
275         return -EINVAL;
276
277     cmd[0] = &switch_cmds[i];
278
279     if ((ret = ioctl_DiseqcSendMsg (p_input, frontend_fd,
280           (i % 2) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13,
281           cmd,
282           (i/2) % 2 ? SEC_TONE_ON : SEC_TONE_OFF,
283           (i/4) % 2 ? SEC_MINI_B : SEC_MINI_A))<0)
284     {
285         msg_Err(p_input, "ioctl_DiseqcSendMsg() failed (%d)", ret);
286         return ret;
287     }
288
289     return ret;
290 }
291
292 /*****************************************************************************
293  * ioctl_SetQPSKFrontend : controls the FE device
294  *****************************************************************************/
295 int ioctl_SetQPSKFrontend (input_thread_t * p_input, struct dvb_frontend_parameters fep, int b_polarisation, 
296                        unsigned int u_lnb_lof1, unsigned int u_lnb_lof2, unsigned int u_lnb_slof,
297                        unsigned int u_adapter, unsigned int u_device  )
298 {
299     int ret;
300     int front;
301     int hiband;
302     char frontend[] = FRONTEND;
303     int i_len;
304
305     i_len = sizeof(FRONTEND);
306     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
307     {
308         msg_Err(p_input,  "DVB-S: FrontEnd snprintf() truncated string for FRONTEND" );
309         frontend[sizeof(FRONTEND)] = '\0';
310     }
311     
312     /* Open the frontend device */
313     msg_Dbg(p_input, "DVB-S: Opening frontend %s", frontend);
314     if(( front = open(frontend,O_RDWR)) < 0)
315     {
316 #   ifdef HAVE_ERRNO_H
317         msg_Err(p_input, "DVB-S: failed to open frontend (%s)", strerror(errno));
318 #   else
319         msg_Err(p_input, "DVB-S: failed to open frontend");
320 #   endif
321         return -1;
322     }
323     
324     /* Set the frequency of the transponder, taking into account the
325        local frequencies of the LNB */
326     hiband = (fep.frequency >= u_lnb_slof);
327
328     if ((ret=ioctl_SetupSwitch (p_input, front, 0, b_polarisation, hiband))<0)
329     {
330         msg_Err(p_input, "DVB-S: Setup frontend switch failed (%d)", ret);
331         return -1;
332     }
333
334     if (hiband)
335         fep.frequency -= u_lnb_lof2;
336     else
337         fep.frequency -= u_lnb_lof1;
338
339     /* Now send it all to the frontend device */
340     if ((ret=ioctl(front, FE_SET_FRONTEND, &fep)) < 0)
341     {
342         close(front);
343 #   ifdef HAVE_ERRNO_H
344         msg_Err(p_input, "DVB-S: setting frontend failed (%d) %s", ret, strerror(errno));
345 #   else
346         msg_Err(p_input, "DVB-S: setting frontend  failed (%d)", ret);
347 #   endif
348         return -1;
349     }
350
351     ret = ioctl_CheckFrontend(p_input, front);
352
353     /* Fixme: Return this instead of closing it.
354        Close front end device */
355     close(front);
356     return ret;
357 }
358
359 int ioctl_SetOFDMFrontend (input_thread_t * p_input, struct dvb_frontend_parameters fep, 
360                             unsigned int u_adapter, unsigned int u_device )
361 {
362     int ret;
363     int front;
364     char frontend[] = FRONTEND;
365     int i_len;
366
367     i_len = sizeof(FRONTEND);
368     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
369     {
370         msg_Err(p_input,  "DVB-T FrontEnd snprintf() truncated string for FRONTEND" );
371         frontend[sizeof(FRONTEND)] = '\0';
372     }
373
374     /* Open the frontend device */
375     msg_Dbg(p_input, "DVB-T: Opening frontend %s", frontend);
376     if(( front = open(frontend,O_RDWR)) < 0)
377     {
378 #   ifdef HAVE_ERRNO_H
379         msg_Err(p_input, "DVB-T: failed to open frontend (%s)", strerror(errno));
380 #   else
381         msg_Err(p_input, "DVB-T: failed to open frontend");
382 #   endif
383         return -1;
384     }
385
386     /* Now send it all to the frontend device */
387     if ((ret=ioctl(front, FE_SET_FRONTEND, &fep)) < 0)
388     {
389         close(front);
390 #   ifdef HAVE_ERRNO_H
391         msg_Err(p_input, "DVB-T: setting frontend failed (%d) %s", ret, strerror(errno));
392 #   else
393         msg_Err(p_input, "DVB-T: setting frontend failed (%d)", ret);
394 #   endif
395         return -1;
396     }
397
398     ret = ioctl_CheckFrontend(p_input, front);
399
400     /* Fixme: Return this instead of closing it.
401        Close front end device */
402     close(front);
403     return ret;
404 }
405
406 int ioctl_SetQAMFrontend (input_thread_t * p_input, struct dvb_frontend_parameters fep, 
407                           unsigned int u_adapter, unsigned int u_device )
408 {
409     int ret;
410     int front;
411     char frontend[] = FRONTEND;
412     int i_len;
413
414     i_len = sizeof(FRONTEND);
415     if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
416     {
417         msg_Err(p_input,  "DVB-C: FrontEnd snprintf() truncated string for FRONTEND" );
418         frontend[sizeof(FRONTEND)] = '\0';
419     }
420
421     /* Open the frontend device */
422     msg_Dbg(p_input, "DVB-C: Opening frontend %s", frontend);
423     if(( front = open(frontend,O_RDWR)) < 0)
424     {
425 #   ifdef HAVE_ERRNO_H
426         msg_Err(p_input, "DVB-C: failed to open frontend (%s)", strerror(errno));
427 #   else
428         msg_Err(p_input, "DVB-C: failed to open frontend");
429 #   endif
430         return -1;
431     }
432
433     /* Now send it all to the frontend device */
434     if ((ret=ioctl(front, FE_SET_FRONTEND, &fep)) < 0)
435     {
436         close(front);
437 #   ifdef HAVE_ERRNO_H
438         msg_Err(p_input, "DVB-C: setting frontend failed (%d) %s", ret, strerror(errno));
439 #   else
440         msg_Err(p_input, "DVB-C: setting frontend failed (%d)", ret);
441 #   endif
442         return -1;
443     }
444
445     /* Check Status of frontend */
446     ret = ioctl_CheckFrontend(p_input, front);
447
448     /* Fixme: Return this instead of closing it.
449        Close front end device */
450     close(front);
451     return ret;
452 }
453
454 /******************************************************************
455  * Check completion of the frontend control sequence
456  ******************************************************************/
457 static int ioctl_CheckFrontend(input_thread_t * p_input, int front)
458 {
459     int i;
460     int ret;
461     struct pollfd pfd[1];
462     struct dvb_frontend_event event;
463     /* poll for frontend event to check if tuning worked */
464     pfd[0].fd = front;
465     pfd[0].events = POLLIN;
466
467 #if 1
468     for (i=0; i<3; i++)
469     {
470         fe_status_t status;
471         if ((ret=ioctl(front, FE_READ_STATUS, &status))<0)
472         {
473 #       ifdef HAVE_ERRNO_H
474             msg_Err(p_input, "reading frontend status failed (%d) %s", ret, strerror(errno));
475 #       else
476             msg_Err(p_input, "reading frontend status failed (%d)", ret);
477 #       endif
478         }
479
480         if (status & FE_HAS_SIGNAL) /* found something above the noise level */
481             msg_Dbg(p_input, "check frontend ... has signal");
482
483         if (status & FE_HAS_CARRIER) /* found a DVB signal  */
484             msg_Dbg(p_input, "check frontend ... has carrier");
485
486         if (status & FE_HAS_VITERBI) /* FEC is stable  */
487             msg_Dbg(p_input, "check frontend ... has stable fec");
488
489         if (status & FE_HAS_SYNC)    /* found sync bytes  */
490             msg_Dbg(p_input, "check frontend ... has sync");
491
492         if (status & FE_HAS_LOCK)    /* everything's working... */
493         {
494             msg_Dbg(p_input, "check frontend ... has lock");
495             msg_Dbg(p_input, "check frontend ... tuning status == 0x%02x!!! ..."
496                              "tuning succeeded", status);
497             return 0;
498         }        
499
500         if (status & FE_TIMEDOUT)    /*  no lock within the last ~2 seconds */
501         {
502              msg_Dbg(p_input, "check frontend ... tuning status == 0x%02x!!! ..."
503                               "tuning failed", status);
504              msg_Err(p_input, "check frontend ... timed out");
505              return -2;
506         }
507
508         if (status & FE_REINIT)
509         {
510             /*  frontend was reinitialized,  */
511             /*  application is recommned to reset */
512             /*  DiSEqC, tone and parameters */
513             msg_Dbg(p_input, "DVB-S: tuning status == 0x%02x!!! ..."
514                              "tuning failed", status);
515             msg_Err(p_input, "check frontend ... resend frontend parameters");
516             return -1;
517         }
518         usleep( 500000 );
519     }
520 #else
521
522     if (poll(pfd,1,3000))
523     {
524         if (pfd[0].revents & POLLIN)
525         {
526             if ( (ret=ioctl(front, FE_GET_EVENT, &event)) < 0)
527             {
528 #           ifdef HAVE_ERRNO_H
529                 msg_Err(p_input, "check frontend ... error occured (%d) %s", ret, strerror(errno));
530 #           else
531                 msg_Err(p_input, "check frontend ... error occured (%d)", ret);
532 #           endif
533                 return -5;
534             }
535
536             switch(event.status)
537             {
538                 case FE_HAS_SIGNAL:  /* found something above the noise level */
539                     msg_Dbg(p_input, "check frontend ... has signal");
540                     break;
541                 case FE_HAS_CARRIER: /* found a DVB signal  */
542                     msg_Dbg(p_input, "check frontend ... has carrier");
543                     break;
544                 case FE_HAS_VITERBI: /* FEC is stable  */
545                     msg_Dbg(p_input, "check frontend ... has stable fec");
546                     break;
547                 case FE_HAS_SYNC:    /* found sync bytes  */
548                     msg_Dbg(p_input, "check frontend ... has sync");
549                     break;
550                 case FE_HAS_LOCK:    /* everything's working... */
551                     msg_Dbg(p_input, "check frontend ... has lock");
552                     return 0;
553                 case FE_TIMEDOUT:    /*  no lock within the last ~2 seconds */
554                     msg_Err(p_input, "check frontend ... timed out");
555                     return -2;
556                 case FE_REINIT:      /*  frontend was reinitialized,  */
557                                      /*  application is recommned to reset */
558                                      /*  DiSEqC, tone and parameters */
559                     msg_Err(p_input, "check frontend ... resend frontend parameters");
560                     return -1;
561             }
562         }
563         else
564         {
565             /* should come here */
566             msg_Err(p_input, "check frontend ... no event occured");
567             return -3;
568         }
569     }
570     else
571     {
572 #   ifdef HAVE_ERRNO_H
573         msg_Err(p_input, "check frontend ... timeout when polling for event (%s)", strerror(errno));
574 #   else
575         msg_Err(p_input, "check frontend ... timeout when polling for event ");
576 #   endif
577         return -4;
578     }
579 #endif
580
581     return -1;
582 }
583
584 /*****************************************************************************
585  * ioctl_SetDMXFilter : controls the demux to add a filter
586  *****************************************************************************/
587 int ioctl_SetDMXFilter(input_thread_t * p_input, int i_pid, int * pi_fd , int i_type,
588                        unsigned int u_adapter, unsigned int u_device )
589 {
590     struct dmx_pes_filter_params s_filter_params;
591     char dmx[] = DMX;
592     int i_len;
593     int result;
594
595     /* We first open the device */
596     i_len = sizeof(DMX);
597     if (snprintf( dmx, sizeof(DMX), DMX, u_adapter, u_device) >= i_len)
598     {
599         msg_Err(p_input,  "snprintf() truncated string for DMX" );
600         dmx[sizeof(DMX)] = '\0';
601     }
602
603     msg_Dbg(p_input, "Opening demux device %s", dmx);
604     if (( (*pi_fd) = open(dmx, O_RDWR|O_NONBLOCK))  < 0)
605     {
606 #   ifdef HAVE_ERRNO_H
607         msg_Err(p_input, "ioctl_SetDMXFilter: opening device failed (%s)", strerror(errno));
608 #   else
609         msg_Err(p_input, "ioctl_SetDMXFilter: opening device failed");
610 #   endif
611         return -1;
612     }
613
614     /* We fill the DEMUX structure : */
615     s_filter_params.pid     =   i_pid;
616     s_filter_params.input   =   DMX_IN_FRONTEND;
617     s_filter_params.output  =   DMX_OUT_TS_TAP;
618     switch ( i_type )
619     {   /* First device */
620         case 1:
621             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO0 for PMT %d", i_pid);
622             s_filter_params.pes_type = DMX_PES_VIDEO0;
623             break;
624         case 2:
625             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO0 for PMT %d", i_pid);
626             s_filter_params.pes_type = DMX_PES_AUDIO0;
627             break;
628         case 3: 
629             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_TELETEXT0 for PMT %d", i_pid);
630             s_filter_params.pes_type = DMX_PES_TELETEXT0;
631             break;
632         case 4: 
633             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_SUBTITLE0 for PMT %d", i_pid);
634             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
635             break;
636         case 5: 
637             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_PCR0 for PMT %d", i_pid);
638             s_filter_params.pes_type = DMX_PES_PCR0;
639             break;
640         /* Second device */    
641         case 6:
642             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO1 for PMT %d", i_pid);
643             s_filter_params.pes_type = DMX_PES_VIDEO1;
644             break;
645         case 7:
646             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO1 for PMT %d", i_pid);
647             s_filter_params.pes_type = DMX_PES_AUDIO1;
648             break;            
649         case 8: 
650             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_TELETEXT1 for PMT %d", i_pid);
651             s_filter_params.pes_type = DMX_PES_TELETEXT1;
652             break;
653         case 9: 
654             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_SUBTITLE1 for PMT %d", i_pid);
655             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
656             break;
657         case 10: 
658             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_PCR1 for PMT %d", i_pid);
659             s_filter_params.pes_type = DMX_PES_PCR1;
660             break;
661         /* Third device */
662         case 11:
663             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO2 for PMT %d", i_pid);
664             s_filter_params.pes_type = DMX_PES_VIDEO2;
665             break;
666         case 12:
667             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO2 for PMT %d", i_pid);
668             s_filter_params.pes_type = DMX_PES_AUDIO2;
669             break;            
670         case 13: 
671             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_TELETEXT2 for PMT %d", i_pid);
672             s_filter_params.pes_type = DMX_PES_TELETEXT2;
673             break;        
674         case 14: 
675             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_SUBTITLE2 for PMT %d", i_pid);
676             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
677             break;
678         case 15: 
679             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_PCR2 for PMT %d", i_pid);
680             s_filter_params.pes_type = DMX_PES_PCR2;
681             break;
682         /* Forth device */    
683         case 16:
684             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_VIDEO3 for PMT %d", i_pid);
685             s_filter_params.pes_type = DMX_PES_VIDEO3;
686             break;
687         case 17:
688             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_AUDIO3 for PMT %d", i_pid);
689             s_filter_params.pes_type = DMX_PES_AUDIO3;
690             break;
691         case 18: 
692             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_TELETEXT3 for PMT %d", i_pid);
693             s_filter_params.pes_type = DMX_PES_TELETEXT3;
694             break;
695         case 19: 
696             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_SUBTITLE3 for PMT %d", i_pid);
697             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
698             break;
699         case 20: 
700             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_PCR3 for PMT %d", i_pid);
701             s_filter_params.pes_type = DMX_PES_PCR3;
702             break;
703         /* Usually used by Nova cards */    
704         case 21:
705             msg_Dbg(p_input, "ioctl_SetDMXFilter: DMX_PES_OTHER for PMT %d", i_pid);
706             s_filter_params.pes_type = DMX_PES_OTHER;
707             break;
708         /* What to do with i? */    
709         default:
710             msg_Err(p_input, "trying to set PMT id to=%d for unknown type %d", i_pid, i_type );
711             break;
712     }
713     s_filter_params.flags = DMX_IMMEDIATE_START;
714
715     /* We then give the order to the device : */
716     if ((result = ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params)) < 0)
717     {
718 #   ifdef HAVE_ERRNO_H
719         msg_Err(p_input, "ioctl_SetDMXFilter: ioctl failed with %d (%s)",result, strerror(errno));
720 #   else
721         msg_Err(p_input, "ioctl_SetDMXFilter: ioctl failed with %d",result);
722 #   endif
723         return -1;
724     }
725     return 0;
726 }
727
728 /*****************************************************************************
729  * ioctl_UnsetDMXFilter : removes a filter
730  *****************************************************************************/
731 int ioctl_UnsetDMXFilter(input_thread_t * p_input, int pi_fd)
732 {
733     int ret;
734     
735     if ((ret=ioctl( pi_fd, DMX_STOP))<0)
736     {
737 #   ifdef HAVE_ERRNO_H
738         msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d) %s", pi_fd, ret, strerror(errno));
739 #   else
740         msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d)", pi_fd, ret);
741 #   endif
742         return -1;
743     }
744     msg_Dbg( p_input, "ioctl_UnsetDMXFilter closing demux %d", pi_fd);
745     close(pi_fd);
746     return 0;
747 }
748
749 /*****************************************************************************
750  * dvb_DecodeBandwidth : decodes arguments for DVB S/C/T card
751  *****************************************************************************/
752 fe_bandwidth_t dvb_DecodeBandwidth(input_thread_t * p_input, int bandwidth)
753 {
754     fe_bandwidth_t      fe_bandwidth = 0;
755     
756     switch (bandwidth)
757     {
758         case 0:
759             fe_bandwidth = BANDWIDTH_AUTO;
760             break;
761         case 6:
762             fe_bandwidth = BANDWIDTH_6_MHZ;
763             break;
764         case 7:
765             fe_bandwidth = BANDWIDTH_7_MHZ;
766             break;
767         case 8:
768             fe_bandwidth = BANDWIDTH_8_MHZ;
769             break;
770         default:
771             msg_Dbg( p_input, "terrestrial dvb has bandwidth not set, using auto");
772             fe_bandwidth = BANDWIDTH_AUTO;
773             break;
774     }
775     
776     return fe_bandwidth;
777 }
778
779 fe_code_rate_t dvb_DecodeFEC(input_thread_t * p_input, int fec)
780 {
781     fe_code_rate_t fe_fec = FEC_NONE;
782     
783     switch( fec )
784     {
785         case 1:
786             fe_fec = FEC_1_2;
787             break;
788         case 2:
789             fe_fec = FEC_2_3;
790             break;
791         case 3:
792             fe_fec = FEC_3_4;
793             break;
794         case 4:
795             fe_fec = FEC_4_5;
796             break;
797         case 5:
798             fe_fec = FEC_5_6;
799             break;
800         case 6:
801             fe_fec = FEC_6_7;
802             break;
803         case 7:
804             fe_fec = FEC_7_8;
805             break;
806         case 8:
807             fe_fec = FEC_8_9;
808             break;
809         case 9:
810             fe_fec = FEC_AUTO;
811             break;
812         default:
813             /* cannot happen */
814             fe_fec = FEC_NONE;
815             msg_Err( p_input, "argument has invalid FEC (%d)", fec);
816             break;
817     }    
818     return fe_fec;
819 }
820
821 fe_modulation_t dvb_DecodeModulation(input_thread_t * p_input, int modulation)
822 {
823     fe_modulation_t     fe_modulation = 0;
824     
825     switch( modulation )
826     {
827         case -1:
828             fe_modulation = QPSK;
829             break;
830         case 0:
831             fe_modulation = QAM_AUTO;
832             break;
833         case 16:
834             fe_modulation = QAM_16;
835             break;
836         case 32:
837             fe_modulation = QAM_32;
838             break;
839         case 64:
840             fe_modulation = QAM_64;
841             break;
842         case 128:
843             fe_modulation = QAM_128;
844             break;
845         case 256:
846             fe_modulation = QAM_256;
847             break;
848         default:
849             msg_Dbg( p_input, "terrestrial/cable dvb has constellation/modulation not set, using auto");
850             fe_modulation = QAM_AUTO;
851             break;
852     }    
853     return fe_modulation;
854 }
855
856 fe_transmit_mode_t dvb_DecodeTransmission(input_thread_t * p_input, int transmission)
857 {
858     fe_transmit_mode_t  fe_transmission = 0;
859     
860     switch( transmission )
861     {
862         case 0:
863             fe_transmission = TRANSMISSION_MODE_AUTO;
864             break;
865         case 2:
866             fe_transmission = TRANSMISSION_MODE_2K;
867             break;
868         case 8:
869             fe_transmission = TRANSMISSION_MODE_8K;
870             break;
871         default:
872             msg_Dbg( p_input, "terrestrial dvb has transmission mode not set, using auto");
873             fe_transmission = TRANSMISSION_MODE_AUTO;
874             break;
875     }    
876     return fe_transmission;
877 }
878
879 fe_guard_interval_t dvb_DecodeGuardInterval(input_thread_t * p_input, int guard)
880 {
881     fe_guard_interval_t fe_guard = 0;
882
883     switch( guard )
884     {
885         case 0:
886             fe_guard = GUARD_INTERVAL_AUTO;
887             break;
888         case 4:
889             fe_guard = GUARD_INTERVAL_1_4;
890             break;
891         case 8:
892             fe_guard = GUARD_INTERVAL_1_8;
893             break;
894         case 16:
895             fe_guard = GUARD_INTERVAL_1_16;
896             break;
897         case 32:
898             fe_guard = GUARD_INTERVAL_1_32;
899             break;
900         default:
901             msg_Dbg( p_input, "terrestrial dvb has guard interval not set, using auto");
902             fe_guard = GUARD_INTERVAL_AUTO;
903             break;
904     }
905     return fe_guard;
906 }
907
908 fe_hierarchy_t dvb_DecodeHierarchy(input_thread_t * p_input, int hierarchy)
909 {
910     fe_hierarchy_t      fe_hierarchy = 0;
911
912     switch (hierarchy)
913     {
914         case -1:
915             fe_hierarchy = HIERARCHY_NONE;
916             break;
917         case 0:
918             fe_hierarchy = HIERARCHY_AUTO;
919             break;
920         case 1:
921             fe_hierarchy = HIERARCHY_1;
922             break;
923         case 2:
924             fe_hierarchy = HIERARCHY_2;
925             break;
926         case 4:
927             fe_hierarchy = HIERARCHY_4;
928             break;
929         default:
930             msg_Dbg( p_input, "terrestrial dvb has hierarchy not set, using auto");
931             fe_hierarchy = HIERARCHY_AUTO;
932             break;
933     }
934     return fe_hierarchy;
935 }
936
937 fe_spectral_inversion_t dvb_DecodeInversion(input_thread_t * p_input, int inversion)
938 {
939     fe_spectral_inversion_t fe_inversion=0;
940
941     switch (inversion)
942     {
943         case 0:
944             fe_inversion = INVERSION_OFF;
945             break;
946         case 1:
947             fe_inversion = INVERSION_ON;
948             break;
949         case 2:
950             fe_inversion = INVERSION_AUTO;
951             break;
952         default:
953             msg_Dbg( p_input, "dvb has inversion/polarisation not set, using auto");
954             fe_inversion = INVERSION_AUTO;
955             break;
956     }
957     return fe_inversion;
958