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#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/ioport.h>
31#include <linux/delay.h>
32#include <linux/videodev2.h>
33#include <linux/version.h>
34#include <linux/io.h>
35#include <media/v4l2-device.h>
36#include <media/v4l2-ioctl.h>
37
38MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
39MODULE_DESCRIPTION("A driver for the Aztech radio card.");
40MODULE_LICENSE("GPL");
41
42
43
44#ifndef CONFIG_RADIO_AZTECH_PORT
45#define CONFIG_RADIO_AZTECH_PORT -1
46#endif
47
48static int io = CONFIG_RADIO_AZTECH_PORT;
49static int radio_nr = -1;
50static int radio_wait_time = 1000;
51
52module_param(io, int, 0);
53module_param(radio_nr, int, 0);
54MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
55
56#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
57
58struct aztech
59{
60 struct v4l2_device v4l2_dev;
61 struct video_device vdev;
62 int io;
63 int curvol;
64 unsigned long curfreq;
65 int stereo;
66 struct mutex lock;
67};
68
69static struct aztech aztech_card;
70
71static int volconvert(int level)
72{
73 level >>= 14;
74 level &= 3;
75
76
77 switch (level) {
78 case 0:
79 return 0;
80 case 1:
81 return 1;
82 case 2:
83 return 4;
84 case 3:
85 return 5;
86 }
87 return 0;
88}
89
90static void send_0_byte(struct aztech *az)
91{
92 udelay(radio_wait_time);
93 outb_p(2 + volconvert(az->curvol), az->io);
94 outb_p(64 + 2 + volconvert(az->curvol), az->io);
95}
96
97static void send_1_byte(struct aztech *az)
98{
99 udelay (radio_wait_time);
100 outb_p(128 + 2 + volconvert(az->curvol), az->io);
101 outb_p(128 + 64 + 2 + volconvert(az->curvol), az->io);
102}
103
104static int az_setvol(struct aztech *az, int vol)
105{
106 mutex_lock(&az->lock);
107 outb(volconvert(vol), az->io);
108 mutex_unlock(&az->lock);
109 return 0;
110}
111
112
113
114
115
116
117
118
119
120static int az_getsigstr(struct aztech *az)
121{
122 int sig = 1;
123
124 mutex_lock(&az->lock);
125 if (inb(az->io) & 2)
126 sig = 0;
127 mutex_unlock(&az->lock);
128 return sig;
129}
130
131static int az_getstereo(struct aztech *az)
132{
133 int stereo = 1;
134
135 mutex_lock(&az->lock);
136 if (inb(az->io) & 1)
137 stereo = 0;
138 mutex_unlock(&az->lock);
139 return stereo;
140}
141
142static int az_setfreq(struct aztech *az, unsigned long frequency)
143{
144 int i;
145
146 mutex_lock(&az->lock);
147
148 az->curfreq = frequency;
149 frequency += 171200;
150 frequency /= 800;
151
152 send_0_byte(az);
153
154 for (i = 0; i < 13; i++)
155 if (frequency & (1 << i))
156 send_1_byte(az);
157 else
158 send_0_byte(az);
159
160 send_0_byte(az);
161 send_0_byte(az);
162 send_0_byte(az);
163 if (az->stereo)
164 send_1_byte(az);
165 else
166 send_0_byte(az);
167
168 send_1_byte(az);
169 send_0_byte(az);
170 send_0_byte(az);
171 send_1_byte(az);
172 send_0_byte(az);
173 send_1_byte(az);
174
175
176
177 udelay(radio_wait_time);
178 outb_p(128 + 64 + volconvert(az->curvol), az->io);
179
180 mutex_unlock(&az->lock);
181
182 return 0;
183}
184
185static int vidioc_querycap(struct file *file, void *priv,
186 struct v4l2_capability *v)
187{
188 strlcpy(v->driver, "radio-aztech", sizeof(v->driver));
189 strlcpy(v->card, "Aztech Radio", sizeof(v->card));
190 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
191 v->version = RADIO_VERSION;
192 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
193 return 0;
194}
195
196static int vidioc_g_tuner(struct file *file, void *priv,
197 struct v4l2_tuner *v)
198{
199 struct aztech *az = video_drvdata(file);
200
201 if (v->index > 0)
202 return -EINVAL;
203
204 strlcpy(v->name, "FM", sizeof(v->name));
205 v->type = V4L2_TUNER_RADIO;
206
207 v->rangelow = 87 * 16000;
208 v->rangehigh = 108 * 16000;
209 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
210 v->capability = V4L2_TUNER_CAP_LOW;
211 if (az_getstereo(az))
212 v->audmode = V4L2_TUNER_MODE_STEREO;
213 else
214 v->audmode = V4L2_TUNER_MODE_MONO;
215 v->signal = 0xFFFF * az_getsigstr(az);
216
217 return 0;
218}
219
220static int vidioc_s_tuner(struct file *file, void *priv,
221 struct v4l2_tuner *v)
222{
223 return v->index ? -EINVAL : 0;
224}
225
226static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
227{
228 *i = 0;
229 return 0;
230}
231
232static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
233{
234 return i ? -EINVAL : 0;
235}
236
237static int vidioc_g_audio(struct file *file, void *priv,
238 struct v4l2_audio *a)
239{
240 a->index = 0;
241 strlcpy(a->name, "Radio", sizeof(a->name));
242 a->capability = V4L2_AUDCAP_STEREO;
243 return 0;
244}
245
246static int vidioc_s_audio(struct file *file, void *priv,
247 struct v4l2_audio *a)
248{
249 return a->index ? -EINVAL : 0;
250}
251
252static int vidioc_s_frequency(struct file *file, void *priv,
253 struct v4l2_frequency *f)
254{
255 struct aztech *az = video_drvdata(file);
256
257 az_setfreq(az, f->frequency);
258 return 0;
259}
260
261static int vidioc_g_frequency(struct file *file, void *priv,
262 struct v4l2_frequency *f)
263{
264 struct aztech *az = video_drvdata(file);
265
266 f->type = V4L2_TUNER_RADIO;
267 f->frequency = az->curfreq;
268 return 0;
269}
270
271static int vidioc_queryctrl(struct file *file, void *priv,
272 struct v4l2_queryctrl *qc)
273{
274 switch (qc->id) {
275 case V4L2_CID_AUDIO_MUTE:
276 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
277 case V4L2_CID_AUDIO_VOLUME:
278 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
279 }
280 return -EINVAL;
281}
282
283static int vidioc_g_ctrl(struct file *file, void *priv,
284 struct v4l2_control *ctrl)
285{
286 struct aztech *az = video_drvdata(file);
287
288 switch (ctrl->id) {
289 case V4L2_CID_AUDIO_MUTE:
290 if (az->curvol == 0)
291 ctrl->value = 1;
292 else
293 ctrl->value = 0;
294 return 0;
295 case V4L2_CID_AUDIO_VOLUME:
296 ctrl->value = az->curvol * 6554;
297 return 0;
298 }
299 return -EINVAL;
300}
301
302static int vidioc_s_ctrl(struct file *file, void *priv,
303 struct v4l2_control *ctrl)
304{
305 struct aztech *az = video_drvdata(file);
306
307 switch (ctrl->id) {
308 case V4L2_CID_AUDIO_MUTE:
309 if (ctrl->value)
310 az_setvol(az, 0);
311 else
312 az_setvol(az, az->curvol);
313 return 0;
314 case V4L2_CID_AUDIO_VOLUME:
315 az_setvol(az, ctrl->value);
316 return 0;
317 }
318 return -EINVAL;
319}
320
321static const struct v4l2_file_operations aztech_fops = {
322 .owner = THIS_MODULE,
323 .ioctl = video_ioctl2,
324};
325
326static const struct v4l2_ioctl_ops aztech_ioctl_ops = {
327 .vidioc_querycap = vidioc_querycap,
328 .vidioc_g_tuner = vidioc_g_tuner,
329 .vidioc_s_tuner = vidioc_s_tuner,
330 .vidioc_g_audio = vidioc_g_audio,
331 .vidioc_s_audio = vidioc_s_audio,
332 .vidioc_g_input = vidioc_g_input,
333 .vidioc_s_input = vidioc_s_input,
334 .vidioc_g_frequency = vidioc_g_frequency,
335 .vidioc_s_frequency = vidioc_s_frequency,
336 .vidioc_queryctrl = vidioc_queryctrl,
337 .vidioc_g_ctrl = vidioc_g_ctrl,
338 .vidioc_s_ctrl = vidioc_s_ctrl,
339};
340
341static int __init aztech_init(void)
342{
343 struct aztech *az = &aztech_card;
344 struct v4l2_device *v4l2_dev = &az->v4l2_dev;
345 int res;
346
347 strlcpy(v4l2_dev->name, "aztech", sizeof(v4l2_dev->name));
348 az->io = io;
349
350 if (az->io == -1) {
351 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x350 or 0x358\n");
352 return -EINVAL;
353 }
354
355 if (!request_region(az->io, 2, "aztech")) {
356 v4l2_err(v4l2_dev, "port 0x%x already in use\n", az->io);
357 return -EBUSY;
358 }
359
360 res = v4l2_device_register(NULL, v4l2_dev);
361 if (res < 0) {
362 release_region(az->io, 2);
363 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
364 return res;
365 }
366
367 mutex_init(&az->lock);
368 strlcpy(az->vdev.name, v4l2_dev->name, sizeof(az->vdev.name));
369 az->vdev.v4l2_dev = v4l2_dev;
370 az->vdev.fops = &aztech_fops;
371 az->vdev.ioctl_ops = &aztech_ioctl_ops;
372 az->vdev.release = video_device_release_empty;
373 video_set_drvdata(&az->vdev, az);
374
375 if (video_register_device(&az->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
376 v4l2_device_unregister(v4l2_dev);
377 release_region(az->io, 2);
378 return -EINVAL;
379 }
380
381 v4l2_info(v4l2_dev, "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
382
383 outb(0, az->io);
384 return 0;
385}
386
387static void __exit aztech_exit(void)
388{
389 struct aztech *az = &aztech_card;
390
391 video_unregister_device(&az->vdev);
392 v4l2_device_unregister(&az->v4l2_dev);
393 release_region(az->io, 2);
394}
395
396module_init(aztech_init);
397module_exit(aztech_exit);
398