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