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 *****************************************************************************/
28 #include <sys/ioctl.h>
32 #ifdef HAVE_INTTYPES_H
33 # include <inttypes.h> /* int16_t .. */
35 #include <sys/types.h>
40 #include <sys/types.h>
44 /* DVB Card Drivers */
45 #include <linux/dvb/dmx.h>
46 #include <linux/dvb/frontend.h>
48 #include <linux/errno.h>
52 static int ioctl_CheckQPSK( int );
54 /*****************************************************************************
55 * ioctl_FrontendControl : commands the SEC device
56 *****************************************************************************/
57 int ioctl_FrontendControl( int freq, int pol, int lnb_slof, int diseqc, unsigned int u_adapter, unsigned int u_device)
59 struct dvb_diseqc_master_cmd cmd;
60 fe_sec_tone_mode_t tone;
61 fe_sec_voltage_t voltage;
63 char front[] = FRONTEND;
66 printf("ioclt_FrontEndControl: enter\n");
67 i_len = sizeof(FRONTEND);
68 if (snprintf(front, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
70 printf( "error: snprintf() truncated string for FRONTEND" );
71 front[sizeof(FRONTEND)] = '\0';
74 printf("ioclt_FrontEndControl: Opening frontend %s\n",front);
75 if((frontend = open(front,O_RDWR)) < 0)
80 /* Set the frequency of the transponder, taking into account the
81 local frequencies of the LNB */
82 tone = (freq<lnb_slof) ? SEC_TONE_OFF : SEC_TONE_ON;
84 /* Set the polarisation of the transponder by setting the correct
85 voltage on the universal LNB */
86 voltage = (pol) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13;
88 /* In case we have a DiSEqC, set it to the correct address */
89 cmd.msg[0] =0x0; /* framing */
90 cmd.msg[1] =0x10; /* address */
91 cmd.msg[2] =0x38; /* command */
92 /* command parameters start at index 3 */
93 cmd.msg[3] = 0xF0 | ((diseqc * 4) & 0x0F);
96 /* Reset everything before sending. */
97 #define CHECK_IOCTL(X) if(X<0) \
103 CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, SEC_TONE_OFF));
104 CHECK_IOCTL(ioctl(frontend, FE_SET_VOLTAGE, voltage));
107 /* Send the data to the SEC device to prepare the LNB for tuning */
108 /*intf_Msg("Sec: Sending data\n");*/
109 CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd));
111 CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_BURST, &cmd));
113 CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, tone));
117 printf("ioclt_FrontEndControl: exit\n");
121 /*****************************************************************************
122 * ioctl_InfoFrontend : return information about given frontend
123 *****************************************************************************/
124 int ioctl_InfoFrontend(struct dvb_frontend_info *info, unsigned int u_adapter, unsigned int u_device)
127 char frontend[] = FRONTEND;
130 printf("ioclt_InfoFrontEnd: enter\n");
131 i_len = sizeof(FRONTEND);
132 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
134 printf( "error: snprintf() truncated string for FRONTEND" );
135 frontend[sizeof(FRONTEND)] = '\0';
138 printf("ioclt_InfoFrontEnd: Opening device %s\n", frontend);
139 if((front = open(frontend,O_RDWR)) < 0)
144 /* Determine type of frontend */
145 if (ioctl(front, FE_GET_INFO, info) < 0)
151 printf( "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",
155 info->frequency_stepsize,
156 info->frequency_tolerance,
157 info->symbol_rate_min,
158 info->symbol_rate_max,
159 info->symbol_rate_tolerance,
160 info->notifier_delay );
163 printf("ioclt_InfoFrontEnd: exit\n");
167 // CPR ---> ===================================================================
170 struct dvb_diseqc_master_cmd cmd;
174 struct diseqc_cmd switch_cmds[] = {
175 { { { 0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00 }, 4 }, 0 },
176 { { { 0xe0, 0x10, 0x38, 0xf2, 0x00, 0x00 }, 4 }, 0 },
177 { { { 0xe0, 0x10, 0x38, 0xf1, 0x00, 0x00 }, 4 }, 0 },
178 { { { 0xe0, 0x10, 0x38, 0xf3, 0x00, 0x00 }, 4 }, 0 },
179 { { { 0xe0, 0x10, 0x38, 0xf4, 0x00, 0x00 }, 4 }, 0 },
180 { { { 0xe0, 0x10, 0x38, 0xf6, 0x00, 0x00 }, 4 }, 0 },
181 { { { 0xe0, 0x10, 0x38, 0xf5, 0x00, 0x00 }, 4 }, 0 },
182 { { { 0xe0, 0x10, 0x38, 0xf7, 0x00, 0x00 }, 4 }, 0 },
183 { { { 0xe0, 0x10, 0x38, 0xf8, 0x00, 0x00 }, 4 }, 0 },
184 { { { 0xe0, 0x10, 0x38, 0xfa, 0x00, 0x00 }, 4 }, 0 },
185 { { { 0xe0, 0x10, 0x38, 0xf9, 0x00, 0x00 }, 4 }, 0 },
186 { { { 0xe0, 0x10, 0x38, 0xfb, 0x00, 0x00 }, 4 }, 0 },
187 { { { 0xe0, 0x10, 0x38, 0xfc, 0x00, 0x00 }, 4 }, 0 },
188 { { { 0xe0, 0x10, 0x38, 0xfe, 0x00, 0x00 }, 4 }, 0 },
189 { { { 0xe0, 0x10, 0x38, 0xfd, 0x00, 0x00 }, 4 }, 0 },
190 { { { 0xe0, 0x10, 0x38, 0xff, 0x00, 0x00 }, 4 }, 0 }
194 int diseqc_send_msg (int fd, fe_sec_voltage_t v, struct diseqc_cmd **cmd,
195 fe_sec_tone_mode_t t, fe_sec_mini_cmd_t b)
199 printf("diseqc_send_msg: enter\n");
200 if ((err = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF)))
203 if ((err = ioctl(fd, FE_SET_VOLTAGE, v)))
208 printf("msg: %02x %02x %02x %02x %02x %02x\n",
209 (*cmd)->cmd.msg[0], (*cmd)->cmd.msg[1],
210 (*cmd)->cmd.msg[2], (*cmd)->cmd.msg[3],
211 (*cmd)->cmd.msg[4], (*cmd)->cmd.msg[5]);
213 if ((err = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &(*cmd)->cmd)))
216 msleep((*cmd)->wait);
222 if ((err = ioctl(fd, FE_DISEQC_SEND_BURST, b)))
227 printf("diseqc_send_msg: exit\n");
228 return ioctl(fd, FE_SET_TONE, t);
231 int setup_switch (int frontend_fd, int switch_pos, int voltage_18, int hiband)
233 struct diseqc_cmd *cmd[2] = { NULL, NULL };
234 int i = 4 * switch_pos + 2 * hiband + (voltage_18 ? 1 : 0);
236 printf("setup_switch: enter\n");
237 printf("switch pos %i, %sV, %sband\n",
238 switch_pos, voltage_18 ? "18" : "13", hiband ? "hi" : "lo");
240 printf("index %i\n", i);
242 if (i < 0 || i >= (int)(sizeof(switch_cmds)/sizeof(struct diseqc_cmd)))
245 cmd[0] = &switch_cmds[i];
247 printf("setup_switch: exit\n");
248 return diseqc_send_msg (frontend_fd,
249 i % 2 ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13,
251 (i/2) % 2 ? SEC_TONE_ON : SEC_TONE_OFF,
252 (i/4) % 2 ? SEC_MINI_B : SEC_MINI_A);
255 // <--- CPR ===================================================================
256 #define SWITCHFREQ 11700000
257 #define LOF_HI 10600000
258 #define LOF_LO 9750000
260 /*****************************************************************************
261 * ioctl_SetQPSKFrontend : controls the FE device
262 *****************************************************************************/
263 int ioctl_SetQPSKFrontend ( struct dvb_frontend_parameters fep, int b_polarisation, unsigned int u_adapter, unsigned int u_device )
269 char frontend[] = FRONTEND;
272 printf("ioctl_SetQPSKFrontend: enter\n");
273 i_len = sizeof(FRONTEND);
274 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
276 printf( "error: snprintf() truncated string for FRONTEND" );
277 frontend[sizeof(FRONTEND)] = '\0';
280 printf("ioctl_SetQPSKFrontend: Opening frontend %s\n", frontend);
281 /* Open the frontend device */
282 if((front = open(frontend,O_RDWR)) < 0)
287 /* Set the frequency of the transponder, taking into account the
288 local frequencies of the LNB */
289 hiband = (fep.frequency >= SWITCHFREQ);
290 setup_switch (front, 0, b_polarisation, hiband );
293 fep.frequency -= LOF_HI;
295 fep.frequency -= LOF_LO;
297 /* Now send it all to the frontend device */
298 if (ioctl(front, FE_SET_FRONTEND, &fep) < 0)
300 printf("ioctl_SetQPSKFrontend: ioctl FE_SET_FRONTEND failed\n");
308 ioctl(front, FE_READ_STATUS, &s);
309 printf("ioctl_SetQPSKFrontend: tuning status == 0x%02x!!! ...", s);
312 printf( "tuning succeeded\n" );
317 printf( "tuning failed\n");
323 /* Close front end device */
325 printf("ioctl_SetQPSKFrontend: exit\n");
329 /******************************************************************
330 * Check completion of the frontend control sequence
331 ******************************************************************/
332 static int ioctl_CheckQPSK(int front)
334 struct pollfd pfd[1];
335 struct dvb_frontend_event event;
336 /* poll for QPSK event to check if tuning worked */
338 pfd[0].events = POLLIN;
340 printf("ioctl_CheckQPSK: enter\n");
341 if (poll(pfd,1,3000))
343 if (pfd[0].revents & POLLIN)
345 if ( ioctl(front, FE_GET_EVENT, &event) < 0)
347 printf("ioctl_CheckQPSK: ioctl FE_GET_EVENT failed\n");
353 case FE_HAS_SIGNAL: /* found something above the noise level */
354 printf("ioctl_CheckQPSK: FE_HAS_SIGNAL\n");
356 case FE_HAS_CARRIER: /* found a DVB signal */
357 printf("ioctl_CheckQPSK: FE_HAS_CARRIER\n");
359 case FE_HAS_VITERBI: /* FEC is stable */
360 printf("ioctl_CheckQPSK: FE_HAS_VITERBI\n");
362 case FE_HAS_SYNC: /* found sync bytes */
363 printf("ioctl_CheckQPSK: FE_HAS_SYNC\n");
365 case FE_HAS_LOCK: /* everything's working... */
366 printf("ioctl_CheckQPSK: FE_HAS_LOCK\n");
368 case FE_TIMEDOUT: /* no lock within the last ~2 seconds */
369 printf("ioctl_CheckQPSK: FE_TIMEDOUT\n");
371 case FE_REINIT: /* frontend was reinitialized, */
372 /* application is recommned to reset */
373 /* DiSEqC, tone and parameters */
374 printf("ioctl_CheckQPSK: FE_REINIT\n");
380 /* should come here */
381 printf("ioctl_CheckQPSK: event() failed\n");
387 printf("ioctl_CheckQPSK: poll() failed\n");
391 printf("ioctl_CheckQPSK: exit\n");
395 /*****************************************************************************
396 * ioctl_SetDMXFilter : controls the demux to add a filter
397 *****************************************************************************/
398 int ioctl_SetDMXFilter( int i_pid, int * pi_fd , int i_type, unsigned int u_adapter, unsigned int u_device )
400 struct dmx_pes_filter_params s_filter_params;
405 /* We first open the device */
406 printf("ioctl_SetDMXFIlter: enter\n");
408 if (snprintf( dmx, sizeof(DMX), DMX, u_adapter, u_device) >= i_len)
410 printf( "error: snprintf() truncated string for DMX" );
411 dmx[sizeof(DMX)] = '\0';
414 printf("ioctl_SetDMXFIlter: Opening demux device %s\n", dmx);
415 if ((*pi_fd = open(dmx, O_RDWR|O_NONBLOCK)) < 0)
420 printf( "@@@ Trying to set PMT id to=%d for type %d\n", i_pid, i_type );
421 /* We fill the DEMUX structure : */
422 s_filter_params.pid = i_pid;
423 s_filter_params.input = DMX_IN_FRONTEND;
424 s_filter_params.output = DMX_OUT_TS_TAP;
428 printf("ioctl_SetDMXFIlter: DMX_PES_VIDEO\n");
429 s_filter_params.pes_type = DMX_PES_VIDEO;
432 printf("ioctl_SetDMXFIlter: DMX_PES_AUDIO\n");
433 s_filter_params.pes_type = DMX_PES_AUDIO;
436 printf("ioctl_SetDMXFIlter: DMX_PES_OTHER\n");
437 s_filter_params.pes_type = DMX_PES_OTHER;
440 s_filter_params.flags = DMX_IMMEDIATE_START;
442 /* We then give the order to the device : */
443 if (result = ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params) < 0)
445 printf("ioctl_SetDMXFIlter: ioctl failed with %d (%s)\n",result, strerror(errno));
448 printf("ioctl_SetDMXFIlter: exit\n");
452 /*****************************************************************************
453 * ioctl_UnsetDMXFilter : removes a filter
454 *****************************************************************************/
455 int ioctl_UnsetDMXFilter(int demux)
457 printf("ioctl_UnsetDMXFIlter: enter\n");
458 ioctl(demux, DMX_STOP);
460 printf("ioctl_UnsetDMXFIlter: exit\n");