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
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <linux/videodev2.h>
#include "v4l2.h"
typedef struct _V4L2RadioDev V4L2RadioDev;
struct _V4L2RadioDev
{
struct _RadioDev parent;
int fd;
int freq_fact;
int radio_rangelow;
int radio_rangehigh;
};
static int
v4l2_radio_init(RadioDev *radio_dev, char *device)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
struct v4l2_capability caps;
struct v4l2_tuner tuner;
if ((dev->fd = open(device, O_RDONLY))< 0)
goto err;
/* does this device provide a tuner? */
memset(&caps, 0, sizeof(caps));
if (ioctl(dev->fd, VIDIOC_QUERYCAP, &caps) < 0) {
perror("VIDIOC_QUERYCAP");
goto err;
}
if ((caps.capabilities & V4L2_CAP_TUNER) == 0) {
fprintf(stderr, "Not a radio tuner\n");
goto err;
}
/* check the tuner */
memset(&tuner, 0, sizeof(tuner));
tuner.index = 0;
if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
perror("VIDIOC_G_TUNER");
goto err;
}
if (tuner.type != V4L2_TUNER_RADIO) {
fprintf(stderr, "Not a radio tuner\n");
goto err;
}
/* Does this tuner expect data in 62.5Hz or 62.5kHz multiples? */
dev->radio_rangelow = tuner.rangelow;
dev->radio_rangehigh = tuner.rangehigh;
if ((tuner.capability & V4L2_TUNER_CAP_LOW) != 0)
dev->freq_fact = 16000;
else
dev->freq_fact = 16;
return 1;
err:
if (dev->fd >= 0)
close(dev->fd);
dev->fd = -1;
return 0;
}
static int
v4l2_radio_is_init(RadioDev *radio_dev)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
return (dev->fd >= 0);
}
static void
v4l2_radio_set_freq(RadioDev *radio_dev, float frequency)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
struct v4l2_frequency freq;
if (dev->fd<0)
return;
memset(&freq, 0, sizeof(freq));
freq.tuner = 0;
freq.type = V4L2_TUNER_RADIO;
freq.frequency = frequency * dev->freq_fact;
if (freq.frequency < dev->radio_rangelow ||
freq.frequency > dev->radio_rangehigh)
return;
if (ioctl(dev->fd, VIDIOC_S_FREQUENCY, &freq) < 0)
perror("VIDIOC_S_FREQUENCY");
}
static void
v4l2_radio_mute(RadioDev *radio_dev, int mute)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
struct v4l2_control control;
if (dev->fd<0)
return;
memset(&control, 0, sizeof(control));
control.id = V4L2_CID_AUDIO_MUTE;
control.value = mute;
if (ioctl(dev->fd, VIDIOC_S_CTRL, &control) < 0)
perror("VIDIOC_S_CTRL");
}
static int
v4l2_radio_get_stereo(RadioDev *radio_dev)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
struct v4l2_tuner tuner;
if (dev->fd<0)
return -1;
memset(&tuner, 0, sizeof(tuner));
tuner.index = 0;
if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
perror("VIDIOC_G_TUNER");
return -1;
}
return tuner.audmode == V4L2_TUNER_MODE_STEREO;
}
static int
v4l2_radio_get_signal(RadioDev *radio_dev)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
struct v4l2_tuner tuner;
if (dev->fd<0)
return -1;
memset(&tuner, 0, sizeof(tuner));
tuner.index = 0;
if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
perror("VIDIOC_G_TUNER");
return -1;
}
return tuner.signal >> 13;
}
static void
v4l2_radio_finalize(RadioDev *radio_dev)
{
V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
if (dev->fd >= 0)
close(dev->fd);
free (dev);
}
RadioDev*
v4l2_radio_dev_new (void)
{
RadioDev *dev;
V4L2RadioDev *v4l2_dev;
v4l2_dev = malloc (sizeof (V4L2RadioDev));
v4l2_dev->fd = -1;
dev = (RadioDev*)v4l2_dev;
dev->init = v4l2_radio_init;
dev->is_init = v4l2_radio_is_init;
dev->set_freq = v4l2_radio_set_freq;
dev->mute = v4l2_radio_mute;
dev->get_stereo = v4l2_radio_get_stereo;
dev->get_signal = v4l2_radio_get_signal;
dev->finalize = v4l2_radio_finalize;
return dev;
}
|