-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtuner-dvb-3.c
381 lines (341 loc) · 12.2 KB
/
vtuner-dvb-3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <poll.h>
#include "vtuner-dvb-3.h"
int hw_init(vtuner_hw_t* hw, int adapter, int frontend, int demux, int dvr) {
char devstr[80];
int i;
hw->adapter = adapter;
hw->frontend = frontend;
hw->demux = demux;
hw->frontend_fd = 0;
hw->streaming_fd = 0;
memset(hw->demux_fd, 0, sizeof(hw->demux_fd));
memset(hw->pids, 0xff, sizeof(hw->pids));
hw->num_props = 0;
memset(hw->props, 0x00, sizeof(hw->props));
sprintf( devstr, "/dev/dvb/adapter%d/frontend%d", hw->adapter, hw->frontend);
hw->frontend_fd = open( devstr, O_RDWR);
if(hw->frontend_fd < 0) {
ERROR("failed to open %s\n", devstr);
goto error;
}
if(ioctl(hw->frontend_fd, FE_GET_INFO, &hw->fe_info) != 0) {
ERROR("FE_GET_INFO failed for %s\n", devstr);
goto error;
}
switch(hw->fe_info.type) {
case FE_QPSK:
#if DVB_API_VERSION < 5
hw->type = VT_S;
#else
if( hw->fe_info.caps & ( FE_HAS_EXTENDED_CAPS | FE_CAN_2G_MODULATION ) ) {
hw->type = VT_S2;
} else {
hw->type = VT_S;
}
#endif
break;
case FE_QAM: hw->type = VT_C; break;
case FE_OFDM: hw->type = VT_T; break;
default:
ERROR("Unknown frontend type %d\n", hw->fe_info.type);
goto cleanup_fe;
}
INFO("FE_GET_INFO dvb-type:%d vtuner-type:%d\n", hw->fe_info.type, hw->type);
sprintf( devstr, "/dev/dvb/adapter%d/dvr%d", hw->adapter, dvr);
hw->streaming_fd = open( devstr, O_RDONLY);
if(hw->streaming_fd < 0) {
ERROR("failed to open %s\n", devstr);
goto cleanup_fe;
}
if( fcntl(hw->streaming_fd, F_SETFL, O_NONBLOCK) != 0) {
ERROR("O_NONBLOCK failed for %s\n",devstr);
goto cleanup_fe;
}
sprintf( devstr, "/dev/dvb/adapter%d/demux%d", hw->adapter, demux);
for(i=0; i<MAX_DEMUX; ++i) {
hw->demux_fd[i] = open(devstr, O_RDWR|O_NONBLOCK);
if(hw->demux_fd[i]<0) {
ERROR("failed to open %s\n", devstr);
goto cleanup_demux;
}
if( ioctl(hw->demux_fd[i], DMX_SET_BUFFER_SIZE, 1024*16) != 0 ) {
ERROR("DMX_SET_BUFFER_SIZE failed for %s\n",devstr);
goto cleanup_demux;
}
/*
if( fcntl(hw->demux_fd[i], F_SETFL, O_NONBLOCK) != 0) {
ERROR("O_NONBLOCK failed for %s\n",devstr);
goto cleanup_demux;
}
*/
}
return 1;
cleanup_demux:
for(i=0;i<MAX_DEMUX; ++i)
if(hw->demux_fd[i] > 0)
close(hw->demux_fd[i]);
cleanup_dvr:
close(hw->streaming_fd);
cleanup_fe:
close(hw->frontend_fd);
error:
return 0;
}
void hw_free(vtuner_hw_t *hw) {
if(hw->frontend_fd>0) close(hw->frontend_fd);
if(hw->streaming_fd>0) close(hw->streaming_fd);
int i;
for(i=0;i<MAX_DEMUX; ++i)
if(hw->demux_fd[i] > 0)
close(hw->demux_fd[i]);
}
void print_frontend_parameters(vtuner_hw_t* hw, struct dvb_frontend_parameters* fe_params, char *msg, size_t msgsize) {
switch(hw->type) {
case VT_S2:
case VT_S: snprintf(msg, msgsize, "freq:%d inversion:%d SR:%d FEC:%d\n", \
fe_params->frequency, fe_params->inversion, \
fe_params->u.qpsk.symbol_rate, fe_params->u.qpsk.fec_inner);
break;
case VT_C: snprintf(msg, msgsize, "freq:%d inversion:%d SR:%d FEC:%d MOD:%d\n", \
fe_params->frequency, fe_params->inversion, \
fe_params->u.qam.symbol_rate, fe_params->u.qam.fec_inner, fe_params->u.qam.modulation);
break;
case VT_T: snprintf(msg, msgsize, "freq:%d inversion:%d BW:%d CRHP:%d CRLP:%d\n", \
fe_params->frequency, fe_params->inversion, \
fe_params->u.ofdm.bandwidth, fe_params->u.ofdm.code_rate_HP, fe_params->u.ofdm.code_rate_LP );
break;
}
}
int hw_get_frontend(vtuner_hw_t* hw, struct dvb_frontend_parameters* fe_params) {
int ret;
ret = ioctl(hw->frontend_fd, FE_GET_FRONTEND, fe_params);
if( ret != 0 ) WARN("FE_GET_FRONTEND failed - %m\n");
return ret;
}
int hw_set_frontend(vtuner_hw_t* hw, struct dvb_frontend_parameters* fe_params) {
int ret;
char msg[1024];
print_frontend_parameters(hw, fe_params, msg, sizeof(msg));
INFO("FE_SET_FRONTEND parameters: %s", msg);
#if DVB_API_VERSION < 5
ret = ioctl(hw->frontend_fd, FE_SET_FRONTEND, fe_params);
#else
struct dtv_properties cmdseq = {
.num = 0,
.props = NULL
};
struct dtv_property CLEAR[] = {
{ .cmd = DTV_CLEAR },
};
cmdseq.num = 1;
cmdseq.props = CLEAR;
if( ioctl(hw->frontend_fd, FE_SET_PROPERTY, &cmdseq) != 0 )
WARN("FE_SET_FRONTEND DTV_CLEAR failed - %m\n");
struct dtv_property S[] = {
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBS },
{ .cmd = DTV_FREQUENCY, .u.data = fe_params->frequency },
{ .cmd = DTV_MODULATION, .u.data = QPSK },
{ .cmd = DTV_SYMBOL_RATE, .u.data = fe_params->u.qpsk.symbol_rate },
{ .cmd = DTV_INNER_FEC, .u.data = fe_params->u.qpsk.fec_inner },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
{ .cmd = DTV_ROLLOFF, .u.data = ROLLOFF_AUTO },
{ .cmd = DTV_PILOT, .u.data = PILOT_AUTO },
{ .cmd = DTV_TUNE },
};
switch(hw->type) {
case VT_S:
case VT_S2: {
cmdseq.num = 9;
cmdseq.props = S;
if( ( hw->type == VT_S || hw->type == VT_S2) && fe_params->u.qpsk.fec_inner > FEC_AUTO) {
cmdseq.props[0].u.data = SYS_DVBS2;
switch( fe_params->u.qpsk.fec_inner ) {
case 19: cmdseq.props[2].u.data = PSK_8;
case 10: cmdseq.props[4].u.data = FEC_1_2 ; break;
case 20: cmdseq.props[2].u.data = PSK_8;
case 11: cmdseq.props[4].u.data = FEC_2_3 ; break;
case 21: cmdseq.props[2].u.data = PSK_8;
case 12: cmdseq.props[4].u.data = FEC_3_4 ; break;
case 22: cmdseq.props[2].u.data = PSK_8;
case 13: cmdseq.props[4].u.data = FEC_5_6 ; break;
case 23: cmdseq.props[2].u.data = PSK_8;
case 14: cmdseq.props[4].u.data = FEC_7_8 ; break;
case 24: cmdseq.props[2].u.data = PSK_8;
case 15: cmdseq.props[4].u.data = FEC_8_9 ; break;
case 25: cmdseq.props[2].u.data = PSK_8;
case 16: cmdseq.props[4].u.data = FEC_3_5 ; break;
case 26: cmdseq.props[2].u.data = PSK_8;
case 17: cmdseq.props[4].u.data = FEC_4_5 ; break;
case 27: cmdseq.props[2].u.data = PSK_8;
case 18: cmdseq.props[4].u.data = FEC_9_10; break;
}
switch( fe_params->inversion & 0x0c ) {
case 0: cmdseq.props[6].u.data = ROLLOFF_35; break;
case 4: cmdseq.props[6].u.data = ROLLOFF_25; break;
case 8: cmdseq.props[6].u.data = ROLLOFF_20; break;
default: WARN("ROLLOFF unknnown\n");
}
switch( fe_params->inversion & 0x30 ) {
case 0: cmdseq.props[7].u.data = PILOT_OFF; break;
case 0x10: cmdseq.props[7].u.data = PILOT_ON; break;
case 0x20: cmdseq.props[7].u.data = PILOT_AUTO; break;
default: WARN("PILOT unknown\n");
}
cmdseq.props[5].u.data &= 0x04;
}
DEBUGHW("S2API tuning SYS:%d MOD:%d FEC:%d INV:%d ROLLOFF:%d PILOT:%d\n", cmdseq.props[0].u.data, cmdseq.props[2].u.data, cmdseq.props[4].u.data, cmdseq.props[5].u.data, cmdseq.props[6].u.data, cmdseq.props[7].u.data);
ret=ioctl(hw->frontend_fd, FE_SET_PROPERTY, &cmdseq);
break;
}
case VT_C:
case VT_T: // even If we would have S2API, the old is sufficent to tune
ret = ioctl(hw->frontend_fd, FE_SET_FRONTEND, fe_params);
break;
default:
WARN("tuning not implemented for HW-type:%d (S:%d, S2:%d C:%d T:%d)\n", hw->type, VT_S, VT_S2, VT_C, VT_T);
}
#endif
if( ret != 0 ) WARN("FE_SET_FRONTEND failed %s - %m\n", msg);
return ret;
}
int hw_get_property(vtuner_hw_t* hw, struct dtv_property* prop) {
WARN("FE_GET_PROPERTY: not implemented %d\n", prop->cmd);
return 0;
}
int hw_set_property(vtuner_hw_t* hw, struct dtv_property* prop) {
int ret=0;
#if DVB_API_VERSION < 5
ret = -1;
WARN("FE_SET_PROPERTY is not available\n");
#else
DEBUGHW("FE_SET_PROPERTY %d\n", prop->cmd);
switch( prop->cmd ) {
case DTV_UNDEFINED: break;
case DTV_CLEAR:
hw->num_props = 0;
DEBUGHW("FE_SET_PROPERTY: DTV_CLEAR\n");
break;
case DTV_TUNE: {
hw->props[hw->num_props].cmd = prop->cmd;
hw->props[hw->num_props].u.data = prop->u.data;
++hw->num_props;
struct dtv_properties cmdseq;
cmdseq.num = hw->num_props;
cmdseq.props = hw->props;
DEBUGHW("FE_SET_PROPERTY: DTV_TUNE\n");
ret=ioctl(hw->frontend_fd, FE_SET_PROPERTY, &cmdseq);
if( ret != 0 ) WARN("FE_SET_PROPERTY failed - %m\n");
} break;
case DTV_FREQUENCY:
case DTV_MODULATION:
case DTV_BANDWIDTH_HZ:
case DTV_INVERSION:
case DTV_DISEQC_MASTER:
case DTV_SYMBOL_RATE:
case DTV_INNER_FEC:
case DTV_VOLTAGE:
case DTV_TONE:
case DTV_PILOT:
case DTV_ROLLOFF:
case DTV_DISEQC_SLAVE_REPLY:
case DTV_FE_CAPABILITY_COUNT:
case DTV_FE_CAPABILITY:
case DTV_DELIVERY_SYSTEM:
if(hw->num_props < DTV_IOCTL_MAX_MSGS) {
hw->props[hw->num_props].cmd = prop->cmd;
hw->props[hw->num_props].u.data = prop->u.data;
DEBUGHW("FE_SET_PROPERTY: set %d to %d, %d properties collected\n", hw->props[hw->num_props].cmd, hw->props[hw->num_props].u.data, hw->num_props+1);
++hw->num_props;
} else {
WARN("FE_SET_PROPERTY properties limit (%d) exceeded.\n", DTV_IOCTL_MAX_MSGS);
ret = -1;
} break;
default:
WARN("FE_SET_PROPERTY unknown property %d\n", prop->cmd);
ret = -1;
}
#endif
return ret;
}
int hw_read_status(vtuner_hw_t* hw, __u32* status) {
int ret;
ret = ioctl(hw->frontend_fd, FE_READ_STATUS, status);
if( ret != 0 ) WARN("FE_READ_STATUS failed\n");
return ret;
}
int hw_set_tone(vtuner_hw_t* hw, __u8 tone) {
int ret=0;
ret = ioctl(hw->frontend_fd, FE_SET_TONE, tone);
if( ret != 0 ) WARN("FE_SET_TONE failed - %m\n");
return ret;
}
int hw_set_voltage(vtuner_hw_t* hw, __u8 voltage) {
int ret=0;
if( hw->type == VT_S || hw->type == VT_S2 ) { // Dream supports this on DVB-T, but not plain linux
ret = ioctl(hw->frontend_fd, FE_SET_VOLTAGE, voltage);
if( ret != 0 ) WARN("FE_SET_VOLTAGE failed - %m\n");
}
return ret;
}
int hw_send_diseq_msg(vtuner_hw_t* hw, diseqc_master_cmd_t* cmd) {
int ret=0;
ret=ioctl(hw->frontend_fd, FE_DISEQC_SEND_MASTER_CMD, cmd);
if( ret != 0 ) WARN("FE_DISEQC_SEND_MASTER_CMD failed - %m\n");
return ret;
}
int hw_send_diseq_burst(vtuner_hw_t* hw, __u8 burst) {
int ret=0;
ret=ioctl(hw->frontend_fd, FE_DISEQC_SEND_BURST, burst);
if( ret != 0 ) WARN("FE_DISEQC_SEND_BURST - %m\n");
return ret;
}
int hw_pidlist(vtuner_hw_t* hw, __u16* pidlist) {
int i,j;
struct dmx_pes_filter_params flt;
DEBUGHWI("hw_pidlist befor: ");
for(i=0; i<MAX_DEMUX; ++i) if(hw->pids[i] != 0xffff) DEBUGHWC("%d ", hw->pids[i]);
DEBUGHWF("\n");
DEBUGHWI("hw_pidlist sent: ");
for(i=0; i<MAX_DEMUX; ++i) if(pidlist[i] != 0xffff) DEBUGHWC("%d ", pidlist[i]);
DEBUGHWF("\n");
for(i=0; i<MAX_DEMUX; ++i)
if(hw->pids[i] != 0xffff) {
for(j=0; j<MAX_DEMUX; ++j)
if(hw->pids[i] == pidlist[j])
break;
if(j == MAX_DEMUX) {
ioctl(hw->demux_fd[i], DMX_STOP, 0);
hw->pids[i] = 0xffff;
}
}
for(i=0; i<MAX_DEMUX; ++i)
if(pidlist[i] != 0xffff) {
for(j=0; j<MAX_DEMUX; ++j)
if(pidlist[i] == hw->pids[j])
break;
if(j == MAX_DEMUX) {
for(j=0; j<MAX_DEMUX; ++j)
if(hw->pids[j] == 0xffff)
break;
if(j==MAX_DEMUX) {
WARN("no free demux found. skip pid %d\n",pidlist[i]);
} else {
flt.pid = hw->pids[j] = pidlist[i];
flt.input = DMX_IN_FRONTEND;
flt.pes_type = DMX_PES_OTHER;
flt.output = DMX_OUT_TS_TAP;
flt.flags = DMX_IMMEDIATE_START;
ioctl(hw->demux_fd[j], DMX_SET_PES_FILTER, &flt);
}
}
}
DEBUGHWI("hw_pidlist after: ");
for(i=0; i<MAX_DEMUX; ++i) if(hw->pids[i] != 0xffff) DEBUGHWC("%d ", hw->pids[i]);
DEBUGHWF("\n");
return 0;
}