cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / stream / dvb_tune.c
blob8730c582a07f2b284e5a2a3803a26c72a9a810e5
1 /* dvbtune - tune.c
3 Copyright (C) Dave Chapman 2001,2002
5 Modified for use with MPlayer, for details see the changelog at
6 http://svn.mplayerhq.hu/mplayer/trunk/
7 $Id$
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <sys/ioctl.h>
30 #include <poll.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <errno.h>
35 #include <linux/dvb/dmx.h>
36 #include <linux/dvb/frontend.h>
37 #include "config.h"
38 #include "dvbin.h"
39 #include "dvb_tune.h"
40 #include "mp_msg.h"
44 int dvb_get_tuner_type(int fe_fd)
46 struct dvb_frontend_info fe_info;
48 int res;
50 res = ioctl(fe_fd, FE_GET_INFO, &fe_info);
51 if(res < 0)
53 mp_msg(MSGT_DEMUX, MSGL_ERR, "FE_GET_INFO error: %d, FD: %d\n\n", errno, fe_fd);
54 return 0;
57 switch(fe_info.type)
59 case FE_OFDM:
60 mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-T\n");
61 return TUNER_TER;
63 case FE_QPSK:
64 mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-S\n");
65 return TUNER_SAT;
67 case FE_QAM:
68 mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-C\n");
69 return TUNER_CBL;
71 #ifdef DVB_ATSC
72 case FE_ATSC:
73 mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-ATSC\n");
74 return TUNER_ATSC;
75 #endif
76 default:
77 mp_msg(MSGT_DEMUX, MSGL_ERR, "UNKNOWN TUNER TYPE\n");
78 return 0;
83 int dvb_open_devices(dvb_priv_t *priv, int n, int demux_cnt)
85 int i;
86 char frontend_dev[32], dvr_dev[32], demux_dev[32];
88 sprintf(frontend_dev, "/dev/dvb/adapter%d/frontend0", n);
89 sprintf(dvr_dev, "/dev/dvb/adapter%d/dvr0", n);
90 sprintf(demux_dev, "/dev/dvb/adapter%d/demux0", n);
91 priv->fe_fd = open(frontend_dev, O_RDWR | O_NONBLOCK);
92 if(priv->fe_fd < 0)
94 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING FRONTEND DEVICE %s: ERRNO %d\n", frontend_dev, errno);
95 return 0;
97 priv->demux_fds_cnt = 0;
98 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_OPEN_DEVICES(%d)\n", demux_cnt);
99 for(i = 0; i < demux_cnt; i++)
101 priv->demux_fds[i] = open(demux_dev, O_RDWR | O_NONBLOCK);
102 if(priv->demux_fds[i] < 0)
104 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DEMUX 0: %d\n", errno);
105 return 0;
107 else
109 mp_msg(MSGT_DEMUX, MSGL_V, "OPEN(%d), file %s: FD=%d, CNT=%d\n", i, demux_dev, priv->demux_fds[i], priv->demux_fds_cnt);
110 priv->demux_fds_cnt++;
115 priv->dvr_fd = open(dvr_dev, O_RDONLY| O_NONBLOCK);
116 if(priv->dvr_fd < 0)
118 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DVR DEVICE %s: %d\n", dvr_dev, errno);
119 return 0;
122 return 1;
126 int dvb_fix_demuxes(dvb_priv_t *priv, int cnt)
128 int i;
129 char demux_dev[32];
131 sprintf(demux_dev, "/dev/dvb/adapter%d/demux0", priv->card);
132 mp_msg(MSGT_DEMUX, MSGL_V, "FIX %d -> %d\n", priv->demux_fds_cnt, cnt);
133 if(priv->demux_fds_cnt >= cnt)
135 for(i = priv->demux_fds_cnt-1; i >= cnt; i--)
137 mp_msg(MSGT_DEMUX, MSGL_V, "FIX, CLOSE fd(%d): %d\n", i, priv->demux_fds[i]);
138 close(priv->demux_fds[i]);
140 priv->demux_fds_cnt = cnt;
142 else if(priv->demux_fds_cnt < cnt)
144 for(i = priv->demux_fds_cnt; i < cnt; i++)
146 priv->demux_fds[i] = open(demux_dev, O_RDWR | O_NONBLOCK);
147 mp_msg(MSGT_DEMUX, MSGL_V, "FIX, OPEN fd(%d): %d\n", i, priv->demux_fds[i]);
148 if(priv->demux_fds[i] < 0)
150 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DEMUX 0: %d\n", errno);
151 return 0;
153 else
154 priv->demux_fds_cnt++;
158 return 1;
161 int dvb_set_ts_filt(int fd, uint16_t pid, dmx_pes_type_t pestype)
163 int i;
164 struct dmx_pes_filter_params pesFilterParams;
166 pesFilterParams.pid = pid;
167 pesFilterParams.input = DMX_IN_FRONTEND;
168 pesFilterParams.output = DMX_OUT_TS_TAP;
169 pesFilterParams.pes_type = pestype;
170 pesFilterParams.flags = DMX_IMMEDIATE_START;
172 errno = 0;
173 if ((i = ioctl(fd, DMX_SET_PES_FILTER, &pesFilterParams)) < 0)
175 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR IN SETTING DMX_FILTER %i for fd %d: ERRNO: %d", pid, fd, errno);
176 return 0;
179 mp_msg(MSGT_DEMUX, MSGL_V, "SET PES FILTER ON PID %d to fd %d, RESULT: %d, ERRNO: %d\n", pid, fd, i, errno);
180 return 1;
184 int dvb_demux_stop(int fd)
186 int i;
187 i = ioctl(fd, DMX_STOP);
189 mp_msg(MSGT_DEMUX, MSGL_DBG2, "STOPPING FD: %d, RESULT: %d\n", fd, i);
191 return i == 0;
195 int dvb_demux_start(int fd)
197 int i;
198 i = ioctl(fd, DMX_START);
200 mp_msg(MSGT_DEMUX, MSGL_DBG2, "STARTING FD: %d, RESULT: %d\n", fd, i);
202 return i == 0;
206 static void print_status(fe_status_t festatus)
208 mp_msg(MSGT_DEMUX, MSGL_V, "FE_STATUS:");
209 if (festatus & FE_HAS_SIGNAL) mp_msg(MSGT_DEMUX, MSGL_V," FE_HAS_SIGNAL");
210 if (festatus & FE_TIMEDOUT) mp_msg(MSGT_DEMUX, MSGL_V, " FE_TIMEDOUT");
211 if (festatus & FE_HAS_LOCK) mp_msg(MSGT_DEMUX, MSGL_V, " FE_HAS_LOCK");
212 if (festatus & FE_HAS_CARRIER) mp_msg(MSGT_DEMUX, MSGL_V, " FE_HAS_CARRIER");
213 if (festatus & FE_HAS_VITERBI) mp_msg(MSGT_DEMUX, MSGL_V, " FE_HAS_VITERBI");
214 if (festatus & FE_HAS_SYNC) mp_msg(MSGT_DEMUX, MSGL_V, " FE_HAS_SYNC");
215 mp_msg(MSGT_DEMUX, MSGL_V, "\n");
219 static int check_status(int fd_frontend, int tmout)
221 int32_t strength;
222 fe_status_t festatus;
223 struct pollfd pfd[1];
224 int ok=0, locks=0;
225 time_t tm1, tm2;
227 pfd[0].fd = fd_frontend;
228 pfd[0].events = POLLPRI;
230 mp_msg(MSGT_DEMUX, MSGL_V, "Getting frontend status\n");
231 tm1 = tm2 = time((time_t*) NULL);
232 while(!ok)
234 festatus = 0;
235 if(poll(pfd,1,tmout*1000) > 0)
237 if (pfd[0].revents & POLLPRI)
239 if(ioctl(fd_frontend, FE_READ_STATUS, &festatus) >= 0)
240 if(festatus & FE_HAS_LOCK)
241 locks++;
244 usleep(10000);
245 tm2 = time((time_t*) NULL);
246 if((festatus & FE_TIMEDOUT) || (locks >= 2) || (tm2 - tm1 >= tmout))
247 ok = 1;
250 if(festatus & FE_HAS_LOCK)
252 strength=0;
253 if(ioctl(fd_frontend,FE_READ_BER,&strength) >= 0)
254 mp_msg(MSGT_DEMUX, MSGL_V, "Bit error rate: %d\n",strength);
256 strength=0;
257 if(ioctl(fd_frontend,FE_READ_SIGNAL_STRENGTH,&strength) >= 0)
258 mp_msg(MSGT_DEMUX, MSGL_V, "Signal strength: %d\n",strength);
260 strength=0;
261 if(ioctl(fd_frontend,FE_READ_SNR,&strength) >= 0)
262 mp_msg(MSGT_DEMUX, MSGL_V, "SNR: %d\n",strength);
264 strength=0;
265 if(ioctl(fd_frontend,FE_READ_UNCORRECTED_BLOCKS,&strength) >= 0)
266 mp_msg(MSGT_DEMUX, MSGL_V, "UNC: %d\n",strength);
268 print_status(festatus);
270 else
272 mp_msg(MSGT_DEMUX, MSGL_ERR, "Not able to lock to the signal on the given frequency, timeout: %d\n", tmout);
273 return -1;
275 return 0;
279 struct diseqc_cmd {
280 struct dvb_diseqc_master_cmd cmd;
281 uint32_t wait;
284 static int diseqc_send_msg(int fd, fe_sec_voltage_t v, struct diseqc_cmd *cmd,
285 fe_sec_tone_mode_t t, fe_sec_mini_cmd_t b)
287 if(ioctl(fd, FE_SET_TONE, SEC_TONE_OFF) == -1)
288 return -1;
289 if(ioctl(fd, FE_SET_VOLTAGE, v) == -1)
290 return -1;
291 usleep(15 * 1000);
292 if(ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &cmd->cmd) == -1)
293 return -1;
294 usleep(cmd->wait * 1000);
295 usleep(15 * 1000);
296 if(ioctl(fd, FE_DISEQC_SEND_BURST, b) == -1)
297 return -1;
298 usleep(15 * 1000);
299 if(ioctl(fd, FE_SET_TONE, t) == -1)
300 return -1;
302 return 0;
305 /* digital satellite equipment control,
306 * specification is available from http://www.eutelsat.com/
308 static int do_diseqc(int secfd, int sat_no, int polv, int hi_lo)
310 struct diseqc_cmd cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
312 /* param: high nibble: reset bits, low nibble set bits,
313 * bits are: option, position, polarizaion, band
315 cmd.cmd.msg[3] =
316 0xf0 | (((sat_no * 4) & 0x0f) | (hi_lo ? 1 : 0) | (polv ? 0 : 2));
318 return diseqc_send_msg(secfd, polv ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18,
319 &cmd, hi_lo ? SEC_TONE_ON : SEC_TONE_OFF,
320 (sat_no / 4) % 2 ? SEC_MINI_B : SEC_MINI_A);
323 static int tune_it(int fd_frontend, int fd_sec, unsigned int freq, unsigned int srate, char pol, int tone,
324 fe_spectral_inversion_t specInv, unsigned int diseqc, fe_modulation_t modulation, fe_code_rate_t HP_CodeRate,
325 fe_transmit_mode_t TransmissionMode, fe_guard_interval_t guardInterval, fe_bandwidth_t bandwidth,
326 fe_code_rate_t LP_CodeRate, fe_hierarchy_t hier, int timeout)
328 int res, hi_lo = 0, dfd;
329 struct dvb_frontend_parameters feparams;
330 struct dvb_frontend_info fe_info;
332 mp_msg(MSGT_DEMUX, MSGL_V, "TUNE_IT, fd_frontend %d, fd_sec %d\nfreq %lu, srate %lu, pol %c, tone %i, specInv, diseqc %u, fe_modulation_t modulation,fe_code_rate_t HP_CodeRate, fe_transmit_mode_t TransmissionMode,fe_guard_interval_t guardInterval, fe_bandwidth_t bandwidth\n",
333 fd_frontend, fd_sec, (long unsigned int)freq, (long unsigned int)srate, pol, tone, diseqc);
336 memset(&feparams, 0, sizeof(feparams));
337 if ( (res = ioctl(fd_frontend,FE_GET_INFO, &fe_info) < 0))
339 mp_msg(MSGT_DEMUX, MSGL_FATAL, "FE_GET_INFO FAILED\n");
340 return -1;
343 mp_msg(MSGT_DEMUX, MSGL_V, "Using DVB card \"%s\"\n", fe_info.name);
345 switch(fe_info.type)
347 case FE_OFDM:
348 if (freq < 1000000) freq*=1000UL;
349 feparams.frequency=freq;
350 feparams.inversion=specInv;
351 feparams.u.ofdm.bandwidth=bandwidth;
352 feparams.u.ofdm.code_rate_HP=HP_CodeRate;
353 feparams.u.ofdm.code_rate_LP=LP_CodeRate;
354 feparams.u.ofdm.constellation=modulation;
355 feparams.u.ofdm.transmission_mode=TransmissionMode;
356 feparams.u.ofdm.guard_interval=guardInterval;
357 feparams.u.ofdm.hierarchy_information=hier;
358 mp_msg(MSGT_DEMUX, MSGL_V, "tuning DVB-T to %d Hz, bandwidth: %d\n",freq, bandwidth);
359 break;
360 case FE_QPSK:
361 if (freq > 2200000)
363 // this must be an absolute frequency
364 if (freq < SLOF)
366 freq = feparams.frequency=(freq-LOF1);
367 hi_lo = 0;
369 else
371 freq = feparams.frequency=(freq-LOF2);
372 hi_lo = 1;
375 else
377 // this is an L-Band frequency
378 feparams.frequency=freq;
381 feparams.inversion=specInv;
382 feparams.u.qpsk.symbol_rate=srate;
383 feparams.u.qpsk.fec_inner=HP_CodeRate;
384 dfd = fd_frontend;
386 mp_msg(MSGT_DEMUX, MSGL_V, "tuning DVB-S to Freq: %u, Pol: %c Srate: %d, 22kHz: %s, LNB: %d\n",freq,pol,srate,hi_lo ? "on" : "off", diseqc);
388 if(do_diseqc(dfd, diseqc, (pol == 'V' ? 1 : 0), hi_lo) == 0)
389 mp_msg(MSGT_DEMUX, MSGL_V, "DISEQC SETTING SUCCEDED\n");
390 else
392 mp_msg(MSGT_DEMUX, MSGL_ERR, "DISEQC SETTING FAILED\n");
393 return -1;
395 break;
396 case FE_QAM:
397 mp_msg(MSGT_DEMUX, MSGL_V, "tuning DVB-C to %d, srate=%d\n",freq,srate);
398 feparams.frequency=freq;
399 feparams.inversion=specInv;
400 feparams.u.qam.symbol_rate = srate;
401 feparams.u.qam.fec_inner = HP_CodeRate;
402 feparams.u.qam.modulation = modulation;
403 break;
404 #ifdef DVB_ATSC
405 case FE_ATSC:
406 mp_msg(MSGT_DEMUX, MSGL_V, "tuning ATSC to %d, modulation=%d\n",freq,modulation);
407 feparams.frequency=freq;
408 feparams.u.vsb.modulation = modulation;
409 break;
410 #endif
411 default:
412 mp_msg(MSGT_DEMUX, MSGL_V, "Unknown FE type. Aborting\n");
413 return 0;
415 usleep(100000);
417 if(ioctl(fd_frontend,FE_SET_FRONTEND,&feparams) < 0)
419 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR tuning channel\n");
420 return -1;
423 return check_status(fd_frontend, timeout);
427 int dvb_tune(dvb_priv_t *priv, int freq, char pol, int srate, int diseqc, int tone,
428 fe_spectral_inversion_t specInv, fe_modulation_t modulation, fe_guard_interval_t guardInterval,
429 fe_transmit_mode_t TransmissionMode, fe_bandwidth_t bandWidth, fe_code_rate_t HP_CodeRate,
430 fe_code_rate_t LP_CodeRate, fe_hierarchy_t hier, int timeout)
432 int ris;
434 mp_msg(MSGT_DEMUX, MSGL_INFO, "dvb_tune Freq: %lu\n", (long unsigned int) freq);
436 ris = tune_it(priv->fe_fd, priv->sec_fd, freq, srate, pol, tone, specInv, diseqc, modulation, HP_CodeRate, TransmissionMode, guardInterval, bandWidth, LP_CodeRate, hier, timeout);
438 if(ris != 0)
439 mp_msg(MSGT_DEMUX, MSGL_INFO, "dvb_tune, TUNING FAILED\n");
441 return ris == 0;