]> git.sesse.net Git - vlc/blob - modules/access/dvb/dvb.c
* src/input/input_clock.c: fixed 2 regressions in 0.6.1. These fixes are actually...
[vlc] / modules / access / dvb / dvb.c
1 /*****************************************************************************
2  * dvb.c : functions to control a DVB card under Linux with v4l2
3  *****************************************************************************
4  * Copyright (C) 1998-2003 VideoLAN
5  *
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>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #include <vlc/vlc.h>
27
28 #include <sys/ioctl.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #ifdef HAVE_INTTYPES_H
33 #   include <inttypes.h>                                       /* int16_t .. */
34 #endif
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <time.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/poll.h>
43
44 /* DVB Card Drivers */
45 #include <linux/dvb/dmx.h>
46 #include <linux/dvb/frontend.h>
47
48 #include <linux/errno.h>
49
50 #include "dvb.h"
51
52 static int ioctl_CheckQPSK( int );
53
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)
58 {
59     struct dvb_diseqc_master_cmd  cmd;
60     fe_sec_tone_mode_t tone;
61     fe_sec_voltage_t voltage;
62     int frontend;
63           char front[] = FRONTEND;
64           int i_len;
65
66           printf("ioclt_FrontEndControl: enter\n");
67           i_len = sizeof(FRONTEND);
68                 if (snprintf(front, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
69                 {
70                   printf( "error: snprintf() truncated string for FRONTEND" );
71                         front[sizeof(FRONTEND)] = '\0';
72     }
73
74           printf("ioclt_FrontEndControl: Opening frontend %s\n",front);   
75     if((frontend = open(front,O_RDWR)) < 0)
76     {
77         return -1;
78     }
79
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;
83
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;
87     
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);   
94     cmd.msg_len = 4;
95
96     /* Reset everything before sending. */
97 #define CHECK_IOCTL(X) if(X<0) \
98     { \
99         close(frontend); \
100         return -1;       \
101     }
102     
103     CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, SEC_TONE_OFF));   
104     CHECK_IOCTL(ioctl(frontend, FE_SET_VOLTAGE, voltage));
105     msleep(15);
106     
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));
110     msleep(15);
111     CHECK_IOCTL(ioctl(frontend, FE_DISEQC_SEND_BURST, &cmd));
112     msleep(15);
113     CHECK_IOCTL(ioctl(frontend, FE_SET_TONE, tone));
114 #undef CHECK_IOCTL
115
116     close(frontend);
117     printf("ioclt_FrontEndControl: exit\n");
118     return 0;
119 }
120
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)
125 {
126     int front;
127           char frontend[] = FRONTEND;
128           int i_len;
129
130           printf("ioclt_InfoFrontEnd: enter\n");
131           i_len = sizeof(FRONTEND);
132                 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
133                 {
134                   printf( "error: snprintf() truncated string for FRONTEND" );
135                         frontend[sizeof(FRONTEND)] = '\0';
136     }
137
138           printf("ioclt_InfoFrontEnd: Opening device %s\n", frontend);
139     if((front = open(frontend,O_RDWR)) < 0)
140     {
141         return -1;
142     }
143
144     /* Determine type of frontend */
145     if (ioctl(front, FE_GET_INFO, info) < 0)
146     {
147         close(front);
148         return -1;
149     }
150 #if 1
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",
152                         info->name,
153                         info->frequency_min,
154                         info->frequency_max,
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 );
161 #endif    
162     close(front);
163           printf("ioclt_InfoFrontEnd: exit\n");
164     return 0;
165 }
166
167 // CPR ---> ===================================================================
168
169 struct diseqc_cmd {
170         struct dvb_diseqc_master_cmd cmd;
171         uint32_t wait;
172 };
173
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 }
191 };
192
193
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)
196 {
197         int err;
198
199   printf("diseqc_send_msg: enter\n");
200         if ((err = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF)))
201                 return err;
202
203         if ((err = ioctl(fd, FE_SET_VOLTAGE, v)))
204                 return err;
205
206         msleep(15);
207         while (*cmd) {
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]);
212
213                 if ((err = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &(*cmd)->cmd)))
214                         return err;
215
216                 msleep((*cmd)->wait);
217                 cmd++;
218         }
219
220         msleep(15);
221
222         if ((err = ioctl(fd, FE_DISEQC_SEND_BURST, b)))
223                 return err;
224
225         msleep(15);
226         
227   printf("diseqc_send_msg: exit\n");
228         return ioctl(fd, FE_SET_TONE, t);
229 }
230
231 int setup_switch (int frontend_fd, int switch_pos, int voltage_18, int hiband)
232 {
233         struct diseqc_cmd *cmd[2] = { NULL, NULL };
234         int i = 4 * switch_pos + 2 * hiband + (voltage_18 ? 1 : 0);
235
236   printf("setup_switch: enter\n");
237         printf("switch pos %i, %sV, %sband\n",
238             switch_pos, voltage_18 ? "18" : "13", hiband ? "hi" : "lo");
239
240         printf("index %i\n", i);
241
242         if (i < 0 || i >= (int)(sizeof(switch_cmds)/sizeof(struct diseqc_cmd)))
243                 return -EINVAL;
244
245         cmd[0] = &switch_cmds[i];
246
247   printf("setup_switch: exit\n");
248         return diseqc_send_msg (frontend_fd,
249                                 i % 2 ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13,
250                                 cmd,
251                                 (i/2) % 2 ? SEC_TONE_ON : SEC_TONE_OFF,
252                                 (i/4) % 2 ? SEC_MINI_B : SEC_MINI_A);
253 }
254
255 // <--- CPR ===================================================================
256 #define SWITCHFREQ 11700000
257 #define LOF_HI     10600000
258 #define LOF_LO      9750000
259
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  )
264 {
265     int front;
266     int rc;
267     int i;
268     int hiband;
269           char frontend[] = FRONTEND;
270           int i_len;
271
272           printf("ioctl_SetQPSKFrontend: enter\n");
273           i_len = sizeof(FRONTEND);
274                 if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
275                 {
276                   printf( "error: snprintf() truncated string for FRONTEND" );
277                         frontend[sizeof(FRONTEND)] = '\0';
278     }
279     
280           printf("ioctl_SetQPSKFrontend: Opening frontend %s\n", frontend);
281     /* Open the frontend device */
282     if((front = open(frontend,O_RDWR)) < 0)
283     {
284         return -1;
285     }
286     
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 );
291
292     if (hiband)
293         fep.frequency -= LOF_HI;
294     else
295         fep.frequency -= LOF_LO;
296
297     /* Now send it all to the frontend device */
298     if (ioctl(front, FE_SET_FRONTEND, &fep) < 0)
299     {
300                           printf("ioctl_SetQPSKFrontend: ioctl FE_SET_FRONTEND failed\n");
301         close(front);
302         return -1;
303     }
304
305     for (i=0; i<3; i++)
306     {
307         fe_status_t s;
308         ioctl(front, FE_READ_STATUS, &s);
309         printf("ioctl_SetQPSKFrontend: tuning status == 0x%02x!!! ...", s);
310         if (s & FE_HAS_LOCK)
311         {
312                         printf( "tuning succeeded\n" );
313                   rc = 0;
314         }
315         else
316         {
317                         printf( "tuning failed\n");
318                         rc = -1;
319         }
320         usleep( 500000 );
321     }
322
323     /* Close front end device */
324     close(front);
325           printf("ioctl_SetQPSKFrontend: exit\n");
326     return rc;
327 }
328
329 /******************************************************************
330  * Check completion of the frontend control sequence
331  ******************************************************************/
332 static int ioctl_CheckQPSK(int front)
333 {
334     struct pollfd pfd[1];
335     struct dvb_frontend_event event;
336     /* poll for QPSK event to check if tuning worked */
337     pfd[0].fd = front;
338     pfd[0].events = POLLIN;
339
340           printf("ioctl_CheckQPSK: enter\n");
341     if (poll(pfd,1,3000))
342     {
343         if (pfd[0].revents & POLLIN)
344         {
345             if ( ioctl(front, FE_GET_EVENT, &event) < 0)
346             {
347                                                   printf("ioctl_CheckQPSK: ioctl FE_GET_EVENT failed\n");
348                 return -5;
349             }
350
351             switch(event.status)
352             {
353                 case FE_HAS_SIGNAL:  /* found something above the noise level */
354                                                           printf("ioctl_CheckQPSK: FE_HAS_SIGNAL\n");
355                                                           break;
356                 case FE_HAS_CARRIER: /* found a DVB signal  */
357                                                           printf("ioctl_CheckQPSK: FE_HAS_CARRIER\n");
358                                                           break;
359                 case FE_HAS_VITERBI: /* FEC is stable  */
360                                                           printf("ioctl_CheckQPSK: FE_HAS_VITERBI\n");
361                                                           break;              
362                 case FE_HAS_SYNC:    /* found sync bytes  */
363                                                           printf("ioctl_CheckQPSK: FE_HAS_SYNC\n");
364                                                           break;              
365                 case FE_HAS_LOCK:    /* everything's working... */
366                                                           printf("ioctl_CheckQPSK: FE_HAS_LOCK\n");
367                                                           break;
368                 case FE_TIMEDOUT:    /*  no lock within the last ~2 seconds */
369                                                           printf("ioctl_CheckQPSK: FE_TIMEDOUT\n");
370                   return -2;
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");
375                       return -1;
376             }
377         }
378         else
379         {
380             /* should come here */
381             printf("ioctl_CheckQPSK: event() failed\n");
382             return -3;
383         }
384     }
385     else
386     {
387                           printf("ioctl_CheckQPSK: poll() failed\n");
388         return -4;
389     }
390
391           printf("ioctl_CheckQPSK: exit\n");
392     return 0;
393 }
394
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 )
399 {
400     struct dmx_pes_filter_params s_filter_params;
401     char dmx[] = DMX;
402           int i_len;
403           int result;
404
405     /* We first open the device */
406     printf("ioctl_SetDMXFIlter: enter\n");
407           i_len = sizeof(DMX);
408                 if (snprintf( dmx, sizeof(DMX), DMX, u_adapter, u_device) >= i_len)
409                 {
410                   printf( "error: snprintf() truncated string for DMX" );
411                         dmx[sizeof(DMX)] = '\0';
412     }
413
414     printf("ioctl_SetDMXFIlter: Opening demux device %s\n", dmx);
415     if ((*pi_fd = open(dmx, O_RDWR|O_NONBLOCK))  < 0)
416     {
417         return -1;
418     }
419
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;
425     switch ( i_type )
426     {
427         case 1:
428             printf("ioctl_SetDMXFIlter: DMX_PES_VIDEO\n");
429             s_filter_params.pes_type = DMX_PES_VIDEO;
430             break;
431         case 2:
432             printf("ioctl_SetDMXFIlter: DMX_PES_AUDIO\n");
433             s_filter_params.pes_type = DMX_PES_AUDIO;
434             break;
435         case 3:
436             printf("ioctl_SetDMXFIlter: DMX_PES_OTHER\n");
437             s_filter_params.pes_type = DMX_PES_OTHER;
438             break;
439     }
440     s_filter_params.flags = DMX_IMMEDIATE_START;
441
442     /* We then give the order to the device : */
443     if (result = ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params) < 0)
444     {
445                     printf("ioctl_SetDMXFIlter: ioctl failed with %d (%s)\n",result, strerror(errno));
446         return -1;
447     }
448     printf("ioctl_SetDMXFIlter: exit\n");
449     return 0;
450 }
451
452 /*****************************************************************************
453  * ioctl_UnsetDMXFilter : removes a filter
454  *****************************************************************************/
455 int ioctl_UnsetDMXFilter(int demux)
456 {
457     printf("ioctl_UnsetDMXFIlter: enter\n");
458     ioctl(demux, DMX_STOP);
459     close(demux);
460     printf("ioctl_UnsetDMXFIlter: exit\n");
461     return 0;
462 }