]> git.sesse.net Git - vlc/blob - modules/access/satellite/dvb.c
* all: as announce calls non-standard functions (SLP), remove it from
[vlc] / modules / access / satellite / dvb.c
1 /*****************************************************************************
2  * dvb.c : functions to control a DVB card under Linux
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  *
6  * Authors: Damien Lucas <nitrox@via.ecp.fr>
7  *          Johan Bilien <jobi@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (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, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #include <sys/ioctl.h>
27 #include <stdio.h>
28 #ifdef HAVE_INTTYPES_H
29 #   include <inttypes.h>                                       /* int16_t .. */
30 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/poll.h>
39
40 /* DVB Card Drivers */
41 #include <ost/sec.h>
42 #include <ost/dmx.h>
43 #include <ost/frontend.h>
44
45
46 #include "dvb.h"
47
48 /*****************************************************************************
49  * ioctl_SECControl : commands the SEC device
50  *****************************************************************************/
51
52
53 int ioctl_SECControl( int freq, int pol, int lnb_slof, int diseqc)
54 {
55     struct secCommand scmd;
56     struct secCmdSequence scmds;
57     int sec;
58
59     if((sec = open(SEC,O_RDWR)) < 0)
60     {
61         return -1;
62     }
63
64     /* Set the frequency of the transponder, taking into account the
65        local frequencies of the LNB */
66     scmds.continuousTone = (freq<lnb_slof) ? SEC_TONE_OFF : SEC_TONE_ON;
67
68     /* Set the polarity of the transponder by setting the correct
69        voltage on the universal LNB */
70     scmds.voltage = (pol) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13;
71
72     /* In case we have a DiSEqC, set it to the correct address */
73     scmd.type=0;
74     scmd.u.diseqc.addr=0x10;
75     scmd.u.diseqc.cmd=0x38;
76     scmd.u.diseqc.numParams=1;
77     scmd.u.diseqc.params[0] = 0xF0 | ((diseqc * 4) & 0x0F) | 
78             (scmds.continuousTone == SEC_TONE_ON ? 1 : 0) |
79             (scmds.voltage==SEC_VOLTAGE_18 ? 2 : 0);
80
81     scmds.miniCommand=SEC_MINI_NONE;
82     scmds.numCommands=1;
83     scmds.commands=&scmd;
84
85     /* Send the data to the SEC device to prepare the LNB for tuning  */
86     /*intf_Msg("Sec: Sending data\n");*/
87     if (ioctl(sec, SEC_SEND_SEQUENCE, &scmds) < 0)
88     {
89         return -1;
90     }
91
92     close(sec);
93
94     return 0;
95 }
96
97 static int check_qpsk( int );
98
99 /*****************************************************************************
100  * ioctl_SetQPSKFrontend : controls the FE device
101  *****************************************************************************/
102
103 int ioctl_SetQPSKFrontend (int freq, int srate, int fec,\
104                         int lnb_lof1, int lnb_lof2, int lnb_slof)
105 {
106     FrontendParameters fep;
107     int front;
108     int rc;
109
110     /* Open the frontend device */
111     if((front = open(FRONTEND,O_RDWR)) < 0)
112     {
113         return -1;
114     }
115
116     /* Set the frequency of the transponder, taking into account the
117        local frequencies of the LNB */
118     fep.Frequency = (freq < lnb_slof) ? freq - lnb_lof1 : freq - lnb_lof2; 
119
120  /* Set symbol rate and FEC */
121     fep.u.qpsk.SymbolRate = srate;
122     fep.u.qpsk.FEC_inner = FEC_AUTO;
123
124     /* Now send it all to the frontend device */
125     if (ioctl(front, FE_SET_FRONTEND, &fep) < 0)
126     {
127         return -1;
128     }
129
130     /* Check if it worked */
131     rc=check_qpsk(front);
132
133     /* Close front end device */
134     close(front);
135     
136     return rc;
137 }
138
139
140
141 /******************************************************************
142  * Check completion of the frontend control sequence
143  ******************************************************************/
144 static int check_qpsk(int front)
145 {
146     struct pollfd pfd[1];
147     FrontendEvent event; 
148     /* poll for QPSK event to check if tuning worked */
149     pfd[0].fd = front;
150     pfd[0].events = POLLIN;
151
152     if (poll(pfd,1,3000))
153     {
154         if (pfd[0].revents & POLLIN)
155         {
156             if ( ioctl(front, FE_GET_EVENT, &event) == -EBUFFEROVERFLOW)
157             {
158                 return -5;
159             }
160         
161             switch(event.type)
162             {
163                 case FE_UNEXPECTED_EV:
164                     return -2;
165                 case FE_FAILURE_EV:
166                     return -1;
167                 case FE_COMPLETION_EV:
168                     break;
169             }
170         }
171         else
172         {
173             /* should come here */
174             return -3;
175         }
176     }
177     else
178     {
179         return -4;
180     }
181     
182     return 0;
183 }
184
185
186 /*****************************************************************************
187  * ioctl_SetDMXAudioFilter : controls the demux to add a filter
188  *****************************************************************************/
189
190 int ioctl_SetDMXFilter( int i_pid, int * pi_fd , int i_type ) 
191 {
192     struct dmxPesFilterParams s_filter_params;
193     
194     /* We first open the device */
195     if ((*pi_fd = open(DMX, O_RDWR|O_NONBLOCK))  < 0)
196     {
197         return -1;
198     }
199
200     /* We fill the DEMUX structure : */
201     s_filter_params.pid     =   i_pid;
202     s_filter_params.input   =   DMX_IN_FRONTEND;
203     s_filter_params.output  =   DMX_OUT_TS_TAP;
204     switch ( i_type )
205     {
206         case 1:
207             s_filter_params.pesType =   DMX_PES_VIDEO;
208             break;
209         case 2:
210             s_filter_params.pesType =   DMX_PES_AUDIO;
211             break;
212         case 3:
213             s_filter_params.pesType =   DMX_PES_OTHER;
214             break;
215     }
216     s_filter_params.flags   =   DMX_IMMEDIATE_START;
217
218     /* We then give the order to the device : */
219     if (ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params) < 0)
220     {
221         return -1;
222     }
223
224     return 0;
225 }
226
227 /*****************************************************************************
228  * ioctl_UnsetDMXFilter : removes a filter
229  *****************************************************************************/
230 int ioctl_UnsetDMXFilter(int demux)
231 {
232     ioctl(demux, DMX_STOP);
233     close(demux);
234     return 0;
235 }