1 /* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
5 * http://194.18.155.92/idc/prod2.idc?nr=50753&lang=e
7 * Notes on the hardware
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.
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.
17 * Frequency control is done digitally.
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
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
31 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
34 #include <linux/module.h> /* Modules */
35 #include <linux/init.h> /* Initdata */
36 #include <linux/ioport.h> /* request_region */
37 #include <linux/proc_fs.h> /* radio card status report */
38 #include <linux/seq_file.h>
39 #include <asm/io.h> /* outb, outb_p */
40 #include <asm/uaccess.h> /* copy to/from user */
41 #include <linux/videodev2.h> /* kernel radio structs */
42 #include <media/v4l2-common.h>
43 #include <media/v4l2-ioctl.h>
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"
49 static struct v4l2_queryctrl radio_qctrl[] = {
51 .id = V4L2_CID_AUDIO_MUTE,
56 .type = V4L2_CTRL_TYPE_BOOLEAN,
58 .id = V4L2_CID_AUDIO_VOLUME,
63 .default_value = 0xff,
64 .type = V4L2_CTRL_TYPE_INTEGER,
69 #ifndef CONFIG_RADIO_TYPHOON_PORT
70 #define CONFIG_RADIO_TYPHOON_PORT -1
73 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
74 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
77 #ifndef CONFIG_PROC_FS
78 #undef CONFIG_RADIO_TYPHOON_PROC_FS
81 struct typhoon_device {
86 unsigned long curfreq;
87 unsigned long mutefreq;
91 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
92 static int typhoon_setfreq_generic(struct typhoon_device *dev,
93 unsigned long frequency);
94 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
95 static void typhoon_mute(struct typhoon_device *dev);
96 static void typhoon_unmute(struct typhoon_device *dev);
97 static int typhoon_setvol(struct typhoon_device *dev, int vol);
99 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
101 mutex_lock(&dev->lock);
102 vol >>= 14; /* Map 16 bit to 2 bit */
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. */
106 mutex_unlock(&dev->lock);
109 static int typhoon_setfreq_generic(struct typhoon_device *dev,
110 unsigned long frequency)
112 unsigned long outval;
116 * The frequency transfer curve is not linear. The best fit I could
119 * outval = -155 + exp((f + 15.55) * 0.057))
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.
126 mutex_lock(&dev->lock);
128 outval = (x * x + 2500) / 5000;
129 outval = (outval * x + 5000) / 10000;
130 outval -= (10 * x * x + 10433) / 20866;
131 outval += 4 * x - 11505;
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);
136 mutex_unlock(&dev->lock);
141 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
143 typhoon_setfreq_generic(dev, frequency);
144 dev->curfreq = frequency;
148 static void typhoon_mute(struct typhoon_device *dev)
152 typhoon_setvol_generic(dev, 0);
153 typhoon_setfreq_generic(dev, dev->mutefreq);
157 static void typhoon_unmute(struct typhoon_device *dev)
161 typhoon_setfreq_generic(dev, dev->curfreq);
162 typhoon_setvol_generic(dev, dev->curvol);
166 static int typhoon_setvol(struct typhoon_device *dev, int vol)
168 if (dev->muted && vol != 0) { /* user is unmuting the card */
173 if (vol == dev->curvol) /* requested volume == current */
176 if (vol == 0) { /* volume == 0 means mute the card */
181 typhoon_setvol_generic(dev, vol);
186 static int vidioc_querycap(struct file *file, void *priv,
187 struct v4l2_capability *v)
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;
197 static int vidioc_g_tuner(struct file *file, void *priv,
198 struct v4l2_tuner *v)
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 */
214 static int vidioc_s_tuner(struct file *file, void *priv,
215 struct v4l2_tuner *v)
223 static int vidioc_s_frequency(struct file *file, void *priv,
224 struct v4l2_frequency *f)
226 struct typhoon_device *typhoon = video_drvdata(file);
228 typhoon->curfreq = f->frequency;
229 typhoon_setfreq(typhoon, typhoon->curfreq);
233 static int vidioc_g_frequency(struct file *file, void *priv,
234 struct v4l2_frequency *f)
236 struct typhoon_device *typhoon = video_drvdata(file);
238 f->type = V4L2_TUNER_RADIO;
239 f->frequency = typhoon->curfreq;
244 static int vidioc_queryctrl(struct file *file, void *priv,
245 struct v4l2_queryctrl *qc)
249 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
250 if (qc->id && qc->id == radio_qctrl[i].id) {
251 memcpy(qc, &(radio_qctrl[i]),
259 static int vidioc_g_ctrl(struct file *file, void *priv,
260 struct v4l2_control *ctrl)
262 struct typhoon_device *typhoon = video_drvdata(file);
265 case V4L2_CID_AUDIO_MUTE:
266 ctrl->value = typhoon->muted;
268 case V4L2_CID_AUDIO_VOLUME:
269 ctrl->value = typhoon->curvol;
275 static int vidioc_s_ctrl (struct file *file, void *priv,
276 struct v4l2_control *ctrl)
278 struct typhoon_device *typhoon = video_drvdata(file);
281 case V4L2_CID_AUDIO_MUTE:
283 typhoon_mute(typhoon);
285 typhoon_unmute(typhoon);
287 case V4L2_CID_AUDIO_VOLUME:
288 typhoon_setvol(typhoon, ctrl->value);
294 static int vidioc_g_audio(struct file *file, void *priv,
295 struct v4l2_audio *a)
300 strcpy(a->name, "Radio");
301 a->capability = V4L2_AUDCAP_STEREO;
305 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
311 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
318 static int vidioc_s_audio(struct file *file, void *priv,
319 struct v4l2_audio *a)
326 static struct typhoon_device typhoon_unit =
328 .iobase = CONFIG_RADIO_TYPHOON_PORT,
329 .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
330 .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
333 static int typhoon_exclusive_open(struct inode *inode, struct file *file)
335 return test_and_set_bit(0, &typhoon_unit.in_use) ? -EBUSY : 0;
338 static int typhoon_exclusive_release(struct inode *inode, struct file *file)
340 clear_bit(0, &typhoon_unit.in_use);
344 static const struct file_operations typhoon_fops = {
345 .owner = THIS_MODULE,
346 .open = typhoon_exclusive_open,
347 .release = typhoon_exclusive_release,
348 .ioctl = video_ioctl2,
350 .compat_ioctl = v4l_compat_ioctl32,
355 static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
356 .vidioc_querycap = vidioc_querycap,
357 .vidioc_g_tuner = vidioc_g_tuner,
358 .vidioc_s_tuner = vidioc_s_tuner,
359 .vidioc_g_audio = vidioc_g_audio,
360 .vidioc_s_audio = vidioc_s_audio,
361 .vidioc_g_input = vidioc_g_input,
362 .vidioc_s_input = vidioc_s_input,
363 .vidioc_g_frequency = vidioc_g_frequency,
364 .vidioc_s_frequency = vidioc_s_frequency,
365 .vidioc_queryctrl = vidioc_queryctrl,
366 .vidioc_g_ctrl = vidioc_g_ctrl,
367 .vidioc_s_ctrl = vidioc_s_ctrl,
370 static struct video_device typhoon_radio = {
371 .name = "Typhoon Radio",
372 .fops = &typhoon_fops,
373 .ioctl_ops = &typhoon_ioctl_ops,
374 .release = video_device_release_empty,
377 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
379 static int typhoon_proc_show(struct seq_file *m, void *v)
382 #define MODULEPROCSTRING "Driver loaded as a module"
384 #define MODULEPROCSTRING "Driver compiled into kernel"
388 seq_puts(m, "Load type: " MODULEPROCSTRING "\n\n");
389 seq_printf(m, "frequency = %lu kHz\n",
390 typhoon_unit.curfreq >> 4);
391 seq_printf(m, "volume = %d\n", typhoon_unit.curvol);
392 seq_printf(m, "mute = %s\n", typhoon_unit.muted ?
394 seq_printf(m, "iobase = 0x%x\n", typhoon_unit.iobase);
395 seq_printf(m, "mute frequency = %lu kHz\n",
396 typhoon_unit.mutefreq >> 4);
400 static int typhoon_proc_open(struct inode *inode, struct file *file)
402 return single_open(file, typhoon_proc_show, NULL);
405 static const struct file_operations typhoon_proc_fops = {
406 .owner = THIS_MODULE,
407 .open = typhoon_proc_open,
410 .release = single_release,
412 #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
414 MODULE_AUTHOR("Dr. Henrik Seidel");
415 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
416 MODULE_LICENSE("GPL");
419 static int radio_nr = -1;
421 module_param(io, int, 0);
422 MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
423 module_param(radio_nr, int, 0);
426 static unsigned long mutefreq;
427 module_param(mutefreq, ulong, 0);
428 MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
431 static int __init typhoon_init(void)
435 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
438 typhoon_unit.iobase = io;
440 if (mutefreq < 87000 || mutefreq > 108500) {
441 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
442 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
445 typhoon_unit.mutefreq = mutefreq;
448 printk(KERN_INFO BANNER);
449 mutex_init(&typhoon_unit.lock);
450 io = typhoon_unit.iobase;
451 if (!request_region(io, 8, "typhoon")) {
452 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
453 typhoon_unit.iobase);
457 video_set_drvdata(&typhoon_radio, &typhoon_unit);
458 if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
459 release_region(io, 8);
462 printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
463 printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
464 typhoon_unit.mutefreq);
465 typhoon_unit.mutefreq <<= 4;
467 /* mute card - prevents noisy bootups */
468 typhoon_mute(&typhoon_unit);
470 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
471 if (!proc_create("driver/radio-typhoon", 0, NULL, &typhoon_proc_fops))
472 printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
478 static void __exit typhoon_cleanup_module(void)
481 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
482 remove_proc_entry("driver/radio-typhoon", NULL);
485 video_unregister_device(&typhoon_radio);
486 release_region(io, 8);
489 module_init(typhoon_init);
490 module_exit(typhoon_cleanup_module);