1 /*****************************************************************************
2 * dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2003 VideoLAN
6 * Authors: Damien Lucas <nitrox@via.ecp.fr>
7 * Johan Bilien <jobi@via.ecp.fr>
8 * Jean-Paul Saman <saman@natlab.research.philips.com>
9 * Christopher Ross <ross@natlab.research.philips.com>
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.
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.
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 *****************************************************************************/
27 #include <vlc/input.h>
29 #include <sys/ioctl.h>
36 #ifdef HAVE_INTTYPES_H
37 # include <inttypes.h> /* int16_t .. */
39 #include <sys/types.h>
44 #include <sys/types.h>
48 /* DVB Card Drivers */
49 #include <linux/dvb/dmx.h>
50 #include <linux/dvb/frontend.h>
52 #include <linux/errno.h>
58 struct dvb_diseqc_master_cmd cmd;
62 struct diseqc_cmd_t switch_cmds[] =
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 }
82 static int ioctl_CheckQPSK(input_thread_t * p_input, int front);
84 /*****************************************************************************
85 * ioctl_FrontendControl : commands the SEC device
86 *****************************************************************************/
87 int ioctl_FrontendControl(input_thread_t * p_input, int freq, int pol, int lnb_slof,
88 int diseqc, unsigned int u_adapter, unsigned int u_device)
90 struct dvb_diseqc_master_cmd cmd;
91 fe_sec_tone_mode_t tone;
92 fe_sec_voltage_t voltage;
94 char front[] = FRONTEND;
97 i_len = sizeof(FRONTEND);
98 if (snprintf(front, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
100 msg_Err(p_input, "snprintf() truncated string for FRONTEND" );
101 front[sizeof(FRONTEND)] = '\0';
104 msg_Dbg(p_input, "Opening frontend %s",front);
105 if((frontend = open(front,O_RDWR)) < 0)
108 msg_Err(p_input, "ioctl_FrontEndControl: Opening frontend failed (%s)",strerror(errno));
110 msg_Err(p_input, "ioctl_FrontEndControl: Opening frontend failed");
115 /* Set the frequency of the transponder, taking into account the
116 local frequencies of the LNB */
117 tone = (freq<lnb_slof) ? SEC_TONE_OFF : SEC_TONE_ON;
119 /* Set the polarisation of the transponder by setting the correct
120 voltage on the universal LNB */
121 voltage = (pol) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13;
123 /* In case we have a DiSEqC, set it to the correct address */
124 cmd.msg[0] =0x0; /* framing */
125 cmd.msg[1] =0x10; /* address */
126 cmd.msg[2] =0x38; /* command */
127 /* command parameters start at index 3 */
128 cmd.msg[3] = 0xF0 | ((diseqc * 4) & 0x0F);
131 /* Reset everything before sending. */
133 # define CHECK_IOCTL(X) if(X<0) \
135 msg_Err( p_input, "InfoFrontend: ioctl failed (%s)", strerror(errno)); \
140 # define CHECK_IOCTL(X) if(X<0) \
142 msg_Err( p_input, "InfoFrontend: ioctl failed"); \
148 CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, SEC_TONE_OFF));
149 CHECK_IOCTL(ioctl(frontend, FE_SET_VOLTAGE, voltage));
152 /* Send the data to the SEC device to prepare the LNB for tuning */
153 CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd));
155 CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_BURST, &cmd));
157 CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, tone));
164 /*****************************************************************************
165 * ioctl_InfoFrontend : return information about given frontend
166 *****************************************************************************/
167 int ioctl_InfoFrontend(input_thread_t * p_input, struct dvb_frontend_info *info,
168 unsigned int u_adapter, unsigned int u_device)
172 char frontend[] = FRONTEND;
175 i_len = sizeof(FRONTEND);
176 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
178 msg_Err(p_input, "snprintf() truncated string for FRONTEND" );
179 frontend[sizeof(FRONTEND)] = '\0';
182 msg_Dbg(p_input, "Opening device %s", frontend);
183 if((front = open(frontend,O_RDWR)) < 0)
186 msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed (%s)", strerror(errno));
188 msg_Err(p_input, "ioctl_InfoFrontEnd: opening device failed");
193 /* Determine type of frontend */
194 if ((ret=ioctl(front, FE_GET_INFO, info)) < 0)
198 msg_Err(p_input, "ioctl FE_GET_INFO failed (%d) %s", ret, strerror(errno));
200 msg_Err(p_input, "ioctl FE_GET_INFO failed (%d)", ret);
204 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",
208 info->frequency_stepsize,
209 info->frequency_tolerance,
210 info->symbol_rate_min,
211 info->symbol_rate_max,
212 info->symbol_rate_tolerance,
213 info->notifier_delay );
219 int ioctl_DiseqcSendMsg (input_thread_t *p_input, int fd, fe_sec_voltage_t v, struct diseqc_cmd_t **cmd,
220 fe_sec_tone_mode_t t, fe_sec_mini_cmd_t b)
224 if ((err = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF))<0)
227 msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d) %s", SEC_TONE_ON ? "on" : "off", err, strerror(errno));
229 msg_Err(p_input, "ioclt FE_SET_TONE failed, tone=%s (%d)", SEC_TONE_ON ? "on" : "off", err);
233 if ((err = ioctl(fd, FE_SET_VOLTAGE, v))<0)
236 msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d) %s", v, err, strerror(errno));
238 msg_Err(p_input, "ioclt FE_SET_VOLTAGE failed, voltage=%d (%d)", v, err);
246 msg_Dbg(p_input, "msg: %02x %02x %02x %02x %02x %02x",
247 (*cmd)->cmd.msg[0], (*cmd)->cmd.msg[1],
248 (*cmd)->cmd.msg[2], (*cmd)->cmd.msg[3],
249 (*cmd)->cmd.msg[4], (*cmd)->cmd.msg[5]);
251 if ((err = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &(*cmd)->cmd))<0)
254 msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d) %s", err, strerror(errno));
256 msg_Err(p_input, "ioclt FE_DISEQC_SEND_MASTER_CMD failed (%d)", err);
261 msleep((*cmd)->wait);
267 if ((err = ioctl(fd, FE_DISEQC_SEND_BURST, b))<0)
270 msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d) %s",b, err, strerror(errno));
272 msg_Err(p_input, "ioctl FE_DISEQC_SEND_BURST failed, burst=%d (%d)",b, err);
278 if ((err = ioctl(fd, FE_SET_TONE, t))<0)
281 msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d) %s", t, err, strerror(errno));
283 msg_Err(p_input, "ioctl FE_SET_TONE failed, tone=%d (%d)", t, err);
290 int ioctl_SetupSwitch (input_thread_t *p_input, int frontend_fd, int switch_pos, int voltage_18, int hiband)
293 struct diseqc_cmd_t *cmd[2] = { NULL, NULL };
294 int i = 4 * switch_pos + 2 * hiband + (voltage_18 ? 1 : 0);
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);
300 if ((i < 0) || (i >= (int)(sizeof(switch_cmds)/sizeof(struct diseqc_cmd_t))))
303 cmd[0] = &switch_cmds[i];
305 if ((ret = ioctl_DiseqcSendMsg (p_input, frontend_fd,
306 (i % 2) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13,
308 (i/2) % 2 ? SEC_TONE_ON : SEC_TONE_OFF,
309 (i/4) % 2 ? SEC_MINI_B : SEC_MINI_A))<0)
311 msg_Err(p_input, "ioctl_DiseqcSendMsg() failed (%d)", ret);
318 #define SWITCHFREQ 11700000
319 #define LOF_HI 10600000
320 #define LOF_LO 9750000
322 /*****************************************************************************
323 * ioctl_SetQPSKFrontend : controls the FE device
324 *****************************************************************************/
325 int ioctl_SetQPSKFrontend (input_thread_t * p_input, struct dvb_frontend_parameters fep,
326 int b_polarisation, unsigned int u_adapter, unsigned int u_device )
332 char frontend[] = FRONTEND;
335 i_len = sizeof(FRONTEND);
336 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
338 msg_Err(p_input, "error: snprintf() truncated string for FRONTEND" );
339 frontend[sizeof(FRONTEND)] = '\0';
342 /* Open the frontend device */
343 msg_Dbg(p_input, "Opening frontend %s", frontend);
344 if((front = open(frontend,O_RDWR)) < 0)
347 msg_Err(p_input, "failed to open frontend (%s)", strerror(errno));
349 msg_Err(p_input, "failed to open frontend");
354 /* Set the frequency of the transponder, taking into account the
355 local frequencies of the LNB */
356 hiband = (fep.frequency >= SWITCHFREQ);
357 if ((ret=ioctl_SetupSwitch (p_input, front, 0, b_polarisation, hiband))<0)
359 msg_Err(p_input, "ioctl_SetupSwitch failed (%d)", ret);
364 fep.frequency -= LOF_HI;
366 fep.frequency -= LOF_LO;
368 /* Now send it all to the frontend device */
369 if ((ret=ioctl(front, FE_SET_FRONTEND, &fep)) < 0)
373 msg_Err(p_input, "ioctl_SetQPSKFrontend: ioctl FE_SET_FRONTEND failed (%d) %s", ret, strerror(errno));
375 msg_Err(p_input, "ioctl_SetQPSKFrontend: ioctl FE_SET_FRONTEND failed (%d)", ret);
383 if ((ret=ioctl(front, FE_READ_STATUS, &s))<0)
386 msg_Err(p_input, "ioctl FE_READ_STATUS failed (%d) %s", ret, strerror(errno));
388 msg_Err(p_input, "ioctl FE_READ_STATUS failed (%d)", ret);
394 msg_Dbg(p_input, "ioctl_SetQPSKFrontend: tuning status == 0x%02x!!! ..."
395 "tuning succeeded", s);
400 msg_Dbg(p_input, "ioctl_SetQPSKFrontend: tuning status == 0x%02x!!! ..."
407 /* Close front end device */
412 /******************************************************************
413 * Check completion of the frontend control sequence
414 ******************************************************************/
415 static int ioctl_CheckQPSK(input_thread_t * p_input, int front)
418 struct pollfd pfd[1];
419 struct dvb_frontend_event event;
420 /* poll for QPSK event to check if tuning worked */
422 pfd[0].events = POLLIN;
424 if (poll(pfd,1,3000))
426 if (pfd[0].revents & POLLIN)
428 if ( (ret=ioctl(front, FE_GET_EVENT, &event)) < 0)
431 msg_Err(p_input, "ioctl_CheckQPSK: ioctl FE_GET_EVENT failed (%d) %s", ret, strerror(errno));
433 msg_Err(p_input, "ioctl_CheckQPSK: ioctl FE_GET_EVENT failed (%d)", ret);
440 case FE_HAS_SIGNAL: /* found something above the noise level */
441 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_HAS_SIGNAL");
443 case FE_HAS_CARRIER: /* found a DVB signal */
444 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_HAS_CARRIER");
446 case FE_HAS_VITERBI: /* FEC is stable */
447 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_HAS_VITERBI");
449 case FE_HAS_SYNC: /* found sync bytes */
450 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_HAS_SYNC");
452 case FE_HAS_LOCK: /* everything's working... */
453 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_HAS_LOCK");
455 case FE_TIMEDOUT: /* no lock within the last ~2 seconds */
456 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_TIMEDOUT");
458 case FE_REINIT: /* frontend was reinitialized, */
459 /* application is recommned to reset */
460 /* DiSEqC, tone and parameters */
461 msg_Dbg(p_input, "ioctl_CheckQPSK: FE_REINIT");
467 /* should come here */
468 msg_Err(p_input, "ioctl_CheckQPSK: event() failed");
475 msg_Err(p_input, "ioctl_CheckQPSK: poll() failed (%s)", strerror(errno));
477 msg_Err(p_input, "ioctl_CheckQPSK: poll() failed");
485 /*****************************************************************************
486 * ioctl_SetDMXFilter : controls the demux to add a filter
487 *****************************************************************************/
488 int ioctl_SetDMXFilter(input_thread_t * p_input, int i_pid, int * pi_fd , int i_type,
489 unsigned int u_adapter, unsigned int u_device )
491 struct dmx_pes_filter_params s_filter_params;
496 /* We first open the device */
498 if (snprintf( dmx, sizeof(DMX), DMX, u_adapter, u_device) >= i_len)
500 msg_Err(p_input, "snprintf() truncated string for DMX" );
501 dmx[sizeof(DMX)] = '\0';
504 msg_Dbg(p_input, "Opening demux device %s", dmx);
505 if ((*pi_fd = open(dmx, O_RDWR|O_NONBLOCK)) < 0)
508 msg_Err(p_input, "ioctl_SetDMXFIlter: opening device failed (%s)", strerror(errno));
510 msg_Err(p_input, "ioctl_SetDMXFIlter: opening device failed");
515 /* We fill the DEMUX structure : */
516 s_filter_params.pid = i_pid;
517 s_filter_params.input = DMX_IN_FRONTEND;
518 s_filter_params.output = DMX_OUT_TS_TAP;
522 msg_Dbg(p_input, "ioctl_SetDMXFIlter: DMX_PES_VIDEO for PMT %d", i_pid);
523 s_filter_params.pes_type = DMX_PES_VIDEO;
526 msg_Dbg(p_input, "ioctl_SetDMXFIlter: DMX_PES_AUDIO for PMT %d", i_pid);
527 s_filter_params.pes_type = DMX_PES_AUDIO;
530 msg_Dbg(p_input, "ioctl_SetDMXFIlter: DMX_PES_OTHER for PMT %d", i_pid);
531 s_filter_params.pes_type = DMX_PES_OTHER;
534 msg_Err(p_input, "trying to set PMT id to=%d for unknown type %d", i_pid, i_type );
537 s_filter_params.flags = DMX_IMMEDIATE_START;
539 /* We then give the order to the device : */
540 if ((result = ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params)) < 0)
543 msg_Err(p_input, "ioctl_SetDMXFIlter: ioctl failed with %d (%s)",result, strerror(errno));
545 msg_Err(p_input, "ioctl_SetDMXFIlter: ioctl failed with %d",result);
552 /*****************************************************************************
553 * ioctl_UnsetDMXFilter : removes a filter
554 *****************************************************************************/
555 int ioctl_UnsetDMXFilter(input_thread_t * p_input, int demux)
559 if ((ret=ioctl(demux, DMX_STOP))<0)
562 msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d) %s", demux, ret, strerror(errno));
564 msg_Err(p_input, "ioctl DMX_STOP failed for demux %d (%d)", demux, ret);