1 /* dvbtune - tune.c
2 
3    Copyright (C) Dave Chapman 2001,2002
4 
5    Modified for use with MPlayer, for details see the changelog at
6    http://svn.mplayerhq.hu/mplayer/trunk/
7    $Id: dvb_tune.c 38108 2018-05-06 17:12:31Z reimar $
8 
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.
13 
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.
18 
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
23 
24 */
25 
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"
41 
42 
43 
dvb_get_tuner_type(int fe_fd)44 int dvb_get_tuner_type(int fe_fd)
45 {
46   struct dvb_frontend_info fe_info;
47 
48   int res;
49 
50   res = ioctl(fe_fd, FE_GET_INFO, &fe_info);
51   if(res < 0)
52   {
53   	mp_msg(MSGT_DEMUX, MSGL_ERR, "FE_GET_INFO error: %d, FD: %d\n\n", errno, fe_fd);
54 	return 0;
55   }
56 
57   switch(fe_info.type)
58   {
59 	case FE_OFDM:
60       mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-T\n");
61 	  return TUNER_TER;
62 
63 	case FE_QPSK:
64       mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-S\n");
65 	  return TUNER_SAT;
66 
67 	case FE_QAM:
68       mp_msg(MSGT_DEMUX, MSGL_V, "TUNER TYPE SEEMS TO BE DVB-C\n");
69 	  return TUNER_CBL;
70 
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;
79   }
80 
81 }
82 
dvb_open_devices(dvb_priv_t * priv,int n,int demux_cnt)83 int dvb_open_devices(dvb_priv_t *priv, int n, int demux_cnt)
84 {
85 	int i;
86 	char frontend_dev[32], dvr_dev[32], demux_dev[32];
87 
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)
93 	{
94 		mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING FRONTEND DEVICE %s: ERRNO %d\n", frontend_dev, errno);
95 		return 0;
96 	}
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++)
100 	{
101 		priv->demux_fds[i] = open(demux_dev, O_RDWR | O_NONBLOCK);
102 		if(priv->demux_fds[i] < 0)
103 		{
104 			mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DEMUX 0: %d\n", errno);
105 			return 0;
106 		}
107 		else
108 		{
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++;
111 		}
112 	}
113 
114 
115 	priv->dvr_fd = open(dvr_dev, O_RDONLY| O_NONBLOCK);
116 	if(priv->dvr_fd < 0)
117 	{
118 		mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DVR DEVICE %s: %d\n", dvr_dev, errno);
119 		return 0;
120 	}
121 
122 	return 1;
123 }
124 
125 
dvb_fix_demuxes(dvb_priv_t * priv,int cnt)126 int dvb_fix_demuxes(dvb_priv_t *priv, int cnt)
127 {
128 	int i;
129 	char demux_dev[32];
130 
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)
134 	{
135 		for(i = priv->demux_fds_cnt-1; i >= cnt; i--)
136 		{
137 			mp_msg(MSGT_DEMUX, MSGL_V, "FIX, CLOSE fd(%d): %d\n", i, priv->demux_fds[i]);
138 			close(priv->demux_fds[i]);
139 		}
140 		priv->demux_fds_cnt = cnt;
141 	}
142 	else if(priv->demux_fds_cnt < cnt)
143 	{
144 		for(i = priv->demux_fds_cnt; i < cnt; i++)
145 		{
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)
149 			{
150 				mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR OPENING DEMUX 0: %d\n", errno);
151 				return 0;
152 			}
153 			else
154 				priv->demux_fds_cnt++;
155 		}
156 	}
157 
158 	return 1;
159 }
160 
dvb_set_ts_filt(int fd,uint16_t pid,dmx_pes_type_t pestype)161 int dvb_set_ts_filt(int fd, uint16_t pid, dmx_pes_type_t pestype)
162 {
163 	int i;
164 	struct dmx_pes_filter_params pesFilterParams;
165 
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;
171 
172 	errno = 0;
173 	if ((i = ioctl(fd, DMX_SET_PES_FILTER, &pesFilterParams)) < 0)
174 	{
175 		mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR IN SETTING DMX_FILTER %i for fd %d: ERRNO: %d", pid, fd, errno);
176 		return 0;
177 	}
178 
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;
181 }
182 
183 
dvb_demux_stop(int fd)184 int dvb_demux_stop(int fd)
185 {
186 	int i;
187 	i = ioctl(fd, DMX_STOP);
188 
189 	mp_msg(MSGT_DEMUX, MSGL_DBG2, "STOPPING FD: %d, RESULT: %d\n", fd, i);
190 
191 	return i == 0;
192 }
193 
194 
dvb_demux_start(int fd)195 int dvb_demux_start(int fd)
196 {
197 	int i;
198 	i = ioctl(fd, DMX_START);
199 
200 	mp_msg(MSGT_DEMUX, MSGL_DBG2, "STARTING FD: %d, RESULT: %d\n", fd, i);
201 
202 	return i == 0;
203 }
204 
205 
print_status(fe_status_t festatus)206 static void print_status(fe_status_t festatus)
207 {
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");
216 }
217 
218 
check_status(int fd_frontend,int tmout)219 static int check_status(int fd_frontend, int tmout)
220 {
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;
226 
227 	pfd[0].fd = fd_frontend;
228 	pfd[0].events = POLLPRI;
229 
230 	mp_msg(MSGT_DEMUX, MSGL_V, "Getting frontend status\n");
231 	tm1 = tm2 = time((time_t*) NULL);
232 	while(!ok)
233 	{
234 		festatus = 0;
235 		if(poll(pfd,1,tmout*1000) > 0)
236 		{
237 			if (pfd[0].revents & POLLPRI)
238 			{
239 				if(ioctl(fd_frontend, FE_READ_STATUS, &festatus) >= 0)
240 					if(festatus & FE_HAS_LOCK)
241 						locks++;
242 			}
243 		}
244 		usleep(10000);
245 		tm2 = time((time_t*) NULL);
246 		if((festatus & FE_TIMEDOUT) || (locks >= 2) || (tm2 - tm1 >= tmout))
247 			ok = 1;
248 	}
249 
250 	if(festatus & FE_HAS_LOCK)
251 	{
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);
255 
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);
259 
260 		strength=0;
261 		if(ioctl(fd_frontend,FE_READ_SNR,&strength) >= 0)
262 		mp_msg(MSGT_DEMUX, MSGL_V, "SNR: %d\n",strength);
263 
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);
267 
268 		print_status(festatus);
269 	}
270 	else
271 	{
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;
274 	}
275 	return 0;
276 }
277 
278 
279 struct diseqc_cmd {
280    struct dvb_diseqc_master_cmd cmd;
281    uint32_t wait;
282 };
283 
diseqc_send_msg(int fd,fe_sec_voltage_t v,struct diseqc_cmd * cmd,fe_sec_tone_mode_t t,fe_sec_mini_cmd_t b)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)
286 {
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;
301 
302     return 0;
303 }
304 
305 /* digital satellite equipment control,
306  * specification is available from http://www.eutelsat.com/
307  */
do_diseqc(int secfd,int sat_no,int polv,int hi_lo)308 static int do_diseqc(int secfd, int sat_no, int polv, int hi_lo)
309 {
310    struct diseqc_cmd cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
311 
312    /* param: high nibble: reset bits, low nibble set bits,
313     * bits are: option, position, polarizaion, band
314     */
315    cmd.cmd.msg[3] =
316        0xf0 | (((sat_no * 4) & 0x0f) | (hi_lo ? 1 : 0) | (polv ? 0 : 2));
317 
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);
321 }
322 
tune_it(int fd_frontend,int fd_sec,unsigned int freq,unsigned int srate,char pol,int tone,fe_spectral_inversion_t specInv,unsigned int diseqc,fe_modulation_t modulation,fe_code_rate_t HP_CodeRate,fe_transmit_mode_t TransmissionMode,fe_guard_interval_t guardInterval,fe_bandwidth_t bandwidth,fe_code_rate_t LP_CodeRate,fe_hierarchy_t hier,int timeout)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)
327 {
328   int res, hi_lo, dfd;
329   struct dvb_frontend_parameters feparams;
330   struct dvb_frontend_info fe_info;
331 
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);
334 
335 
336   memset(&feparams, 0, sizeof(feparams));
337   if ( (res = ioctl(fd_frontend,FE_GET_INFO, &fe_info) < 0))
338   {
339         mp_msg(MSGT_DEMUX, MSGL_FATAL, "FE_GET_INFO FAILED\n");
340         return -1;
341   }
342 
343   mp_msg(MSGT_DEMUX, MSGL_V, "Using DVB card \"%s\"\n", fe_info.name);
344 
345   switch(fe_info.type)
346   {
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)
362       {
363         // this must be an absolute frequency
364         if (freq < SLOF)
365         {
366           freq = feparams.frequency=(freq-LOF1);
367           hi_lo = 0;
368         }
369         else
370         {
371           freq = feparams.frequency=(freq-LOF2);
372           hi_lo = 1;
373         }
374       }
375       else
376       {
377         // this is an L-Band frequency
378        feparams.frequency=freq;
379       }
380 
381       feparams.inversion=specInv;
382       feparams.u.qpsk.symbol_rate=srate;
383       feparams.u.qpsk.fec_inner=HP_CodeRate;
384       dfd = fd_frontend;
385 
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);
387 
388       if(do_diseqc(dfd, diseqc, (pol == 'V' ? 1 : 0), hi_lo) == 0)
389           mp_msg(MSGT_DEMUX, MSGL_V, "DISEQC SETTING SUCCEEDED\n");
390       else
391       {
392           mp_msg(MSGT_DEMUX, MSGL_ERR, "DISEQC SETTING FAILED\n");
393           return -1;
394       }
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;
414   }
415   usleep(100000);
416 
417   if(ioctl(fd_frontend,FE_SET_FRONTEND,&feparams) < 0)
418   {
419     mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR tuning channel\n");
420     return -1;
421   }
422 
423   return check_status(fd_frontend, timeout);
424 }
425 
426 
dvb_tune(dvb_priv_t * priv,int freq,char pol,int srate,int diseqc,int tone,fe_spectral_inversion_t specInv,fe_modulation_t modulation,fe_guard_interval_t guardInterval,fe_transmit_mode_t TransmissionMode,fe_bandwidth_t bandWidth,fe_code_rate_t HP_CodeRate,fe_code_rate_t LP_CodeRate,fe_hierarchy_t hier,int 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)
431 {
432 	int ris;
433 
434 	mp_msg(MSGT_DEMUX, MSGL_INFO, "dvb_tune Freq: %lu\n", (long unsigned int) freq);
435 
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);
437 
438 	if(ris != 0)
439 		mp_msg(MSGT_DEMUX, MSGL_INFO, "dvb_tune, TUNING FAILED\n");
440 
441 	return ris == 0;
442 }
443