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/
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
29 #include <sys/ioctl.h>
35 #include <linux/dvb/dmx.h>
36 #include <linux/dvb/frontend.h>
44 int dvb_get_tuner_type(int fe_fd
)
46 struct dvb_frontend_info fe_info
;
50 res
= ioctl(fe_fd
, FE_GET_INFO
, &fe_info
);
53 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "FE_GET_INFO error: %d, FD: %d\n\n", errno
, fe_fd
);
60 mp_msg(MSGT_DEMUX
, MSGL_V
, "TUNER TYPE SEEMS TO BE DVB-T\n");
64 mp_msg(MSGT_DEMUX
, MSGL_V
, "TUNER TYPE SEEMS TO BE DVB-S\n");
68 mp_msg(MSGT_DEMUX
, MSGL_V
, "TUNER TYPE SEEMS TO BE DVB-C\n");
73 mp_msg(MSGT_DEMUX
, MSGL_V
, "TUNER TYPE SEEMS TO BE DVB-ATSC\n");
77 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "UNKNOWN TUNER TYPE\n");
83 int dvb_open_devices(dvb_priv_t
*priv
, int n
, int demux_cnt
)
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
);
94 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "ERROR OPENING FRONTEND DEVICE %s: ERRNO %d\n", frontend_dev
, errno
);
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
);
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
);
118 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "ERROR OPENING DVR DEVICE %s: %d\n", dvr_dev
, errno
);
126 int dvb_fix_demuxes(dvb_priv_t
*priv
, int cnt
)
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
);
154 priv
->demux_fds_cnt
++;
161 int dvb_set_ts_filt(int fd
, uint16_t pid
, dmx_pes_type_t pestype
)
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
;
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
);
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
);
184 int dvb_demux_stop(int fd
)
187 i
= ioctl(fd
, DMX_STOP
);
189 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "STOPPING FD: %d, RESULT: %d\n", fd
, i
);
195 int dvb_demux_start(int fd
)
198 i
= ioctl(fd
, DMX_START
);
200 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "STARTING FD: %d, RESULT: %d\n", fd
, i
);
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
)
222 fe_status_t festatus
;
223 struct pollfd pfd
[1];
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
);
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
)
245 tm2
= time((time_t*) NULL
);
246 if((festatus
& FE_TIMEDOUT
) || (locks
>= 2) || (tm2
- tm1
>= tmout
))
250 if(festatus
& FE_HAS_LOCK
)
253 if(ioctl(fd_frontend
,FE_READ_BER
,&strength
) >= 0)
254 mp_msg(MSGT_DEMUX
, MSGL_V
, "Bit error rate: %d\n",strength
);
257 if(ioctl(fd_frontend
,FE_READ_SIGNAL_STRENGTH
,&strength
) >= 0)
258 mp_msg(MSGT_DEMUX
, MSGL_V
, "Signal strength: %d\n",strength
);
261 if(ioctl(fd_frontend
,FE_READ_SNR
,&strength
) >= 0)
262 mp_msg(MSGT_DEMUX
, MSGL_V
, "SNR: %d\n",strength
);
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
);
272 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Not able to lock to the signal on the given frequency, timeout: %d\n", tmout
);
280 struct dvb_diseqc_master_cmd cmd
;
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)
289 if(ioctl(fd
, FE_SET_VOLTAGE
, v
) == -1)
292 if(ioctl(fd
, FE_DISEQC_SEND_MASTER_CMD
, &cmd
->cmd
) == -1)
294 usleep(cmd
->wait
* 1000);
296 if(ioctl(fd
, FE_DISEQC_SEND_BURST
, b
) == -1)
299 if(ioctl(fd
, FE_SET_TONE
, t
) == -1)
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
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
)
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");
343 mp_msg(MSGT_DEMUX
, MSGL_V
, "Using DVB card \"%s\"\n", fe_info
.name
);
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
);
363 // this must be an absolute frequency
366 freq
= feparams
.frequency
=(freq
-LOF1
);
371 freq
= feparams
.frequency
=(freq
-LOF2
);
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
;
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");
392 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "DISEQC SETTING FAILED\n");
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
;
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
;
412 mp_msg(MSGT_DEMUX
, MSGL_V
, "Unknown FE type. Aborting\n");
417 if(ioctl(fd_frontend
,FE_SET_FRONTEND
,&feparams
) < 0)
419 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "ERROR tuning channel\n");
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
)
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
);
439 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "dvb_tune, TUNING FAILED\n");