V4L/DVB (8783): v4l: add all missing video_device release callbacks
[deliverable/linux.git] / drivers / media / radio / radio-typhoon.c
CommitLineData
1da177e4
LT
1/* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
3 *
4 * Card manufacturer:
5 * http://194.18.155.92/idc/prod2.idc?nr=50753&lang=e
6 *
7 * Notes on the hardware
8 *
9 * This card has two output sockets, one for speakers and one for line.
10 * The speaker output has volume control, but only in four discrete
11 * steps. The line output has neither volume control nor mute.
12 *
13 * The card has auto-stereo according to its manual, although it all
14 * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
15 * antenna - I really don't know for sure.
16 *
17 * Frequency control is done digitally.
18 *
19 * Volume control is done digitally, but there are only four different
20 * possible values. So you should better always turn the volume up and
21 * use line control. I got the best results by connecting line output
22 * to the sound card microphone input. For such a configuration the
23 * volume control has no effect, since volume control only influences
24 * the speaker output.
25 *
26 * There is no explicit mute/unmute. So I set the radio frequency to a
27 * value where I do expect just noise and turn the speaker volume down.
28 * The frequency change is necessary since the card never seems to be
29 * completely silent.
30c48305
MCC
30 *
31 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
32 */
33
34#include <linux/module.h> /* Modules */
35#include <linux/init.h> /* Initdata */
fb911ee8 36#include <linux/ioport.h> /* request_region */
1da177e4 37#include <linux/proc_fs.h> /* radio card status report */
0b9c2b7a 38#include <linux/seq_file.h>
1da177e4
LT
39#include <asm/io.h> /* outb, outb_p */
40#include <asm/uaccess.h> /* copy to/from user */
30c48305 41#include <linux/videodev2.h> /* kernel radio structs */
5e87efa3 42#include <media/v4l2-common.h>
35ea11ff 43#include <media/v4l2-ioctl.h>
1da177e4 44
30c48305
MCC
45#include <linux/version.h> /* for KERNEL_VERSION MACRO */
46#define RADIO_VERSION KERNEL_VERSION(0,1,1)
47#define BANNER "Typhoon Radio Card driver v0.1.1\n"
48
49static struct v4l2_queryctrl radio_qctrl[] = {
50 {
51 .id = V4L2_CID_AUDIO_MUTE,
52 .name = "Mute",
53 .minimum = 0,
54 .maximum = 1,
55 .default_value = 1,
56 .type = V4L2_CTRL_TYPE_BOOLEAN,
57 },{
58 .id = V4L2_CID_AUDIO_VOLUME,
59 .name = "Volume",
60 .minimum = 0,
61 .maximum = 65535,
62 .step = 1<<14,
63 .default_value = 0xff,
64 .type = V4L2_CTRL_TYPE_INTEGER,
65 }
66};
67
1da177e4
LT
68
69#ifndef CONFIG_RADIO_TYPHOON_PORT
70#define CONFIG_RADIO_TYPHOON_PORT -1
71#endif
72
73#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
74#define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
75#endif
76
77#ifndef CONFIG_PROC_FS
78#undef CONFIG_RADIO_TYPHOON_PROC_FS
79#endif
80
81struct typhoon_device {
3ca685aa 82 unsigned long in_use;
1da177e4
LT
83 int iobase;
84 int curvol;
85 int muted;
86 unsigned long curfreq;
87 unsigned long mutefreq;
3593cab5 88 struct mutex lock;
1da177e4
LT
89};
90
91static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
92static int typhoon_setfreq_generic(struct typhoon_device *dev,
93 unsigned long frequency);
94static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
95static void typhoon_mute(struct typhoon_device *dev);
96static void typhoon_unmute(struct typhoon_device *dev);
97static int typhoon_setvol(struct typhoon_device *dev, int vol);
1da177e4
LT
98
99static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
100{
3593cab5 101 mutex_lock(&dev->lock);
1da177e4
LT
102 vol >>= 14; /* Map 16 bit to 2 bit */
103 vol &= 3;
104 outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
105 outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
3593cab5 106 mutex_unlock(&dev->lock);
1da177e4
LT
107}
108
109static int typhoon_setfreq_generic(struct typhoon_device *dev,
110 unsigned long frequency)
111{
112 unsigned long outval;
113 unsigned long x;
114
115 /*
116 * The frequency transfer curve is not linear. The best fit I could
117 * get is
118 *
119 * outval = -155 + exp((f + 15.55) * 0.057))
120 *
121 * where frequency f is in MHz. Since we don't have exp in the kernel,
122 * I approximate this function by a third order polynomial.
123 *
124 */
125
3593cab5 126 mutex_lock(&dev->lock);
1da177e4
LT
127 x = frequency / 160;
128 outval = (x * x + 2500) / 5000;
129 outval = (outval * x + 5000) / 10000;
130 outval -= (10 * x * x + 10433) / 20866;
131 outval += 4 * x - 11505;
132
133 outb_p((outval >> 8) & 0x01, dev->iobase + 4);
134 outb_p(outval >> 9, dev->iobase + 6);
135 outb_p(outval & 0xff, dev->iobase + 8);
3593cab5 136 mutex_unlock(&dev->lock);
1da177e4
LT
137
138 return 0;
139}
140
141static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
142{
143 typhoon_setfreq_generic(dev, frequency);
144 dev->curfreq = frequency;
145 return 0;
146}
147
148static void typhoon_mute(struct typhoon_device *dev)
149{
150 if (dev->muted == 1)
151 return;
152 typhoon_setvol_generic(dev, 0);
153 typhoon_setfreq_generic(dev, dev->mutefreq);
154 dev->muted = 1;
155}
156
157static void typhoon_unmute(struct typhoon_device *dev)
158{
159 if (dev->muted == 0)
160 return;
161 typhoon_setfreq_generic(dev, dev->curfreq);
162 typhoon_setvol_generic(dev, dev->curvol);
163 dev->muted = 0;
164}
165
166static int typhoon_setvol(struct typhoon_device *dev, int vol)
167{
168 if (dev->muted && vol != 0) { /* user is unmuting the card */
169 dev->curvol = vol;
170 typhoon_unmute(dev);
171 return 0;
172 }
173 if (vol == dev->curvol) /* requested volume == current */
174 return 0;
175
176 if (vol == 0) { /* volume == 0 means mute the card */
177 typhoon_mute(dev);
178 dev->curvol = vol;
179 return 0;
180 }
181 typhoon_setvol_generic(dev, vol);
182 dev->curvol = vol;
183 return 0;
184}
185
3f6892ac
DL
186static int vidioc_querycap(struct file *file, void *priv,
187 struct v4l2_capability *v)
188{
189 strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
190 strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
191 sprintf(v->bus_info, "ISA");
192 v->version = RADIO_VERSION;
193 v->capabilities = V4L2_CAP_TUNER;
194 return 0;
195}
196
197static int vidioc_g_tuner(struct file *file, void *priv,
198 struct v4l2_tuner *v)
199{
200 if (v->index > 0)
201 return -EINVAL;
202
203 strcpy(v->name, "FM");
204 v->type = V4L2_TUNER_RADIO;
205 v->rangelow = (87.5*16000);
206 v->rangehigh = (108*16000);
207 v->rxsubchans = V4L2_TUNER_SUB_MONO;
208 v->capability = V4L2_TUNER_CAP_LOW;
209 v->audmode = V4L2_TUNER_MODE_MONO;
210 v->signal = 0xFFFF; /* We can't get the signal strength */
211 return 0;
212}
1da177e4 213
3f6892ac
DL
214static int vidioc_s_tuner(struct file *file, void *priv,
215 struct v4l2_tuner *v)
216{
217 if (v->index > 0)
218 return -EINVAL;
219
220 return 0;
221}
222
223static int vidioc_s_frequency(struct file *file, void *priv,
224 struct v4l2_frequency *f)
1da177e4
LT
225{
226 struct video_device *dev = video_devdata(file);
227 struct typhoon_device *typhoon = dev->priv;
228
3f6892ac
DL
229 typhoon->curfreq = f->frequency;
230 typhoon_setfreq(typhoon, typhoon->curfreq);
231 return 0;
232}
30c48305 233
3f6892ac
DL
234static int vidioc_g_frequency(struct file *file, void *priv,
235 struct v4l2_frequency *f)
236{
237 struct video_device *dev = video_devdata(file);
238 struct typhoon_device *typhoon = dev->priv;
30c48305 239
3f6892ac
DL
240 f->type = V4L2_TUNER_RADIO;
241 f->frequency = typhoon->curfreq;
30c48305 242
3f6892ac
DL
243 return 0;
244}
30c48305 245
3f6892ac
DL
246static int vidioc_queryctrl(struct file *file, void *priv,
247 struct v4l2_queryctrl *qc)
248{
249 int i;
30c48305 250
3f6892ac
DL
251 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
252 if (qc->id && qc->id == radio_qctrl[i].id) {
253 memcpy(qc, &(radio_qctrl[i]),
254 sizeof(*qc));
1da177e4
LT
255 return 0;
256 }
3f6892ac
DL
257 }
258 return -EINVAL;
259}
30c48305 260
3f6892ac
DL
261static int vidioc_g_ctrl(struct file *file, void *priv,
262 struct v4l2_control *ctrl)
263{
264 struct video_device *dev = video_devdata(file);
265 struct typhoon_device *typhoon = dev->priv;
30c48305 266
3f6892ac
DL
267 switch (ctrl->id) {
268 case V4L2_CID_AUDIO_MUTE:
269 ctrl->value = typhoon->muted;
270 return 0;
271 case V4L2_CID_AUDIO_VOLUME:
272 ctrl->value = typhoon->curvol;
273 return 0;
274 }
275 return -EINVAL;
276}
30c48305 277
3f6892ac
DL
278static int vidioc_s_ctrl (struct file *file, void *priv,
279 struct v4l2_control *ctrl)
280{
281 struct video_device *dev = video_devdata(file);
282 struct typhoon_device *typhoon = dev->priv;
30c48305 283
3f6892ac
DL
284 switch (ctrl->id) {
285 case V4L2_CID_AUDIO_MUTE:
286 if (ctrl->value)
287 typhoon_mute(typhoon);
288 else
289 typhoon_unmute(typhoon);
290 return 0;
291 case V4L2_CID_AUDIO_VOLUME:
292 typhoon_setvol(typhoon, ctrl->value);
293 return 0;
294 }
295 return -EINVAL;
296}
30c48305 297
3f6892ac
DL
298static int vidioc_g_audio(struct file *file, void *priv,
299 struct v4l2_audio *a)
300{
301 if (a->index > 1)
302 return -EINVAL;
30c48305 303
3f6892ac
DL
304 strcpy(a->name, "Radio");
305 a->capability = V4L2_AUDCAP_STEREO;
306 return 0;
307}
308
309static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
310{
311 *i = 0;
312 return 0;
1da177e4
LT
313}
314
3f6892ac 315static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1da177e4 316{
3f6892ac
DL
317 if (i != 0)
318 return -EINVAL;
319 return 0;
320}
321
322static int vidioc_s_audio(struct file *file, void *priv,
323 struct v4l2_audio *a)
324{
325 if (a->index != 0)
326 return -EINVAL;
327 return 0;
1da177e4
LT
328}
329
330static struct typhoon_device typhoon_unit =
331{
332 .iobase = CONFIG_RADIO_TYPHOON_PORT,
333 .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
334 .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
335};
336
3ca685aa
HV
337static int typhoon_exclusive_open(struct inode *inode, struct file *file)
338{
339 return test_and_set_bit(0, &typhoon_unit.in_use) ? -EBUSY : 0;
340}
341
342static int typhoon_exclusive_release(struct inode *inode, struct file *file)
343{
344 clear_bit(0, &typhoon_unit.in_use);
345 return 0;
346}
347
fa027c2a 348static const struct file_operations typhoon_fops = {
1da177e4 349 .owner = THIS_MODULE,
3ca685aa
HV
350 .open = typhoon_exclusive_open,
351 .release = typhoon_exclusive_release,
3f6892ac 352 .ioctl = video_ioctl2,
078ff795 353#ifdef CONFIG_COMPAT
0d0fbf81 354 .compat_ioctl = v4l_compat_ioctl32,
078ff795 355#endif
1da177e4
LT
356 .llseek = no_llseek,
357};
358
a399810c 359static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
3f6892ac
DL
360 .vidioc_querycap = vidioc_querycap,
361 .vidioc_g_tuner = vidioc_g_tuner,
362 .vidioc_s_tuner = vidioc_s_tuner,
363 .vidioc_g_audio = vidioc_g_audio,
364 .vidioc_s_audio = vidioc_s_audio,
365 .vidioc_g_input = vidioc_g_input,
366 .vidioc_s_input = vidioc_s_input,
367 .vidioc_g_frequency = vidioc_g_frequency,
368 .vidioc_s_frequency = vidioc_s_frequency,
369 .vidioc_queryctrl = vidioc_queryctrl,
370 .vidioc_g_ctrl = vidioc_g_ctrl,
371 .vidioc_s_ctrl = vidioc_s_ctrl,
1da177e4
LT
372};
373
a399810c 374static struct video_device typhoon_radio = {
a399810c 375 .name = "Typhoon Radio",
a399810c
HV
376 .fops = &typhoon_fops,
377 .ioctl_ops = &typhoon_ioctl_ops,
aa5e90af 378 .release = video_device_release_empty,
a399810c
HV
379};
380
1da177e4
LT
381#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
382
0b9c2b7a 383static int typhoon_proc_show(struct seq_file *m, void *v)
1da177e4 384{
1da177e4
LT
385 #ifdef MODULE
386 #define MODULEPROCSTRING "Driver loaded as a module"
387 #else
388 #define MODULEPROCSTRING "Driver compiled into kernel"
389 #endif
390
0b9c2b7a
AD
391 seq_puts(m, BANNER);
392 seq_puts(m, "Load type: " MODULEPROCSTRING "\n\n");
393 seq_printf(m, "frequency = %lu kHz\n",
1da177e4 394 typhoon_unit.curfreq >> 4);
0b9c2b7a
AD
395 seq_printf(m, "volume = %d\n", typhoon_unit.curvol);
396 seq_printf(m, "mute = %s\n", typhoon_unit.muted ?
1da177e4 397 "on" : "off");
0b9c2b7a
AD
398 seq_printf(m, "iobase = 0x%x\n", typhoon_unit.iobase);
399 seq_printf(m, "mute frequency = %lu kHz\n",
1da177e4 400 typhoon_unit.mutefreq >> 4);
0b9c2b7a 401 return 0;
1da177e4
LT
402}
403
0b9c2b7a
AD
404static int typhoon_proc_open(struct inode *inode, struct file *file)
405{
406 return single_open(file, typhoon_proc_show, NULL);
407}
408
409static const struct file_operations typhoon_proc_fops = {
410 .owner = THIS_MODULE,
411 .open = typhoon_proc_open,
412 .read = seq_read,
413 .llseek = seq_lseek,
414 .release = single_release,
415};
1da177e4
LT
416#endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
417
418MODULE_AUTHOR("Dr. Henrik Seidel");
419MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
420MODULE_LICENSE("GPL");
421
422static int io = -1;
423static int radio_nr = -1;
424
425module_param(io, int, 0);
426MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
427module_param(radio_nr, int, 0);
428
429#ifdef MODULE
ff699e6b 430static unsigned long mutefreq;
1da177e4
LT
431module_param(mutefreq, ulong, 0);
432MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
433#endif
434
435static int __init typhoon_init(void)
436{
437#ifdef MODULE
438 if (io == -1) {
439 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
440 return -EINVAL;
441 }
442 typhoon_unit.iobase = io;
443
444 if (mutefreq < 87000 || mutefreq > 108500) {
445 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
446 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
447 return -EINVAL;
448 }
449 typhoon_unit.mutefreq = mutefreq;
450#endif /* MODULE */
451
452 printk(KERN_INFO BANNER);
3593cab5 453 mutex_init(&typhoon_unit.lock);
1da177e4
LT
454 io = typhoon_unit.iobase;
455 if (!request_region(io, 8, "typhoon")) {
456 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
457 typhoon_unit.iobase);
458 return -EBUSY;
459 }
460
461 typhoon_radio.priv = &typhoon_unit;
3ca685aa 462 if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
1da177e4
LT
463 release_region(io, 8);
464 return -EINVAL;
465 }
466 printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
467 printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
468 typhoon_unit.mutefreq);
469 typhoon_unit.mutefreq <<= 4;
470
471 /* mute card - prevents noisy bootups */
472 typhoon_mute(&typhoon_unit);
473
474#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
0b9c2b7a 475 if (!proc_create("driver/radio-typhoon", 0, NULL, &typhoon_proc_fops))
657de3cd 476 printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
1da177e4
LT
477#endif
478
479 return 0;
480}
481
482static void __exit typhoon_cleanup_module(void)
483{
484
485#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
486 remove_proc_entry("driver/radio-typhoon", NULL);
487#endif
488
489 video_unregister_device(&typhoon_radio);
490 release_region(io, 8);
491}
492
493module_init(typhoon_init);
494module_exit(typhoon_cleanup_module);
495
This page took 0.390191 seconds and 5 git commands to generate.