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/version.h> /* for KERNEL_VERSION MACRO */
38 #include <linux/videodev2.h> /* kernel radio structs */
39 #include <linux/io.h> /* outb, outb_p */
40 #include <linux/uaccess.h> /* copy to/from user */
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-ioctl.h>
44 MODULE_AUTHOR("Dr. Henrik Seidel");
45 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
46 MODULE_LICENSE("GPL");
48 #ifndef CONFIG_RADIO_TYPHOON_PORT
49 #define CONFIG_RADIO_TYPHOON_PORT -1
52 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
53 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
56 static int io = CONFIG_RADIO_TYPHOON_PORT;
57 static int radio_nr = -1;
59 module_param(io, int, 0);
60 MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
62 module_param(radio_nr, int, 0);
64 static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
65 module_param(mutefreq, ulong, 0);
66 MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
68 #define RADIO_VERSION KERNEL_VERSION(0, 1, 1)
70 #define BANNER "Typhoon Radio Card driver v0.1.1\n"
73 struct v4l2_device v4l2_dev;
74 struct video_device vdev;
78 unsigned long curfreq;
79 unsigned long mutefreq;
83 static struct typhoon typhoon_card;
85 static void typhoon_setvol_generic(struct typhoon *dev, int vol)
87 mutex_lock(&dev->lock);
88 vol >>= 14; /* Map 16 bit to 2 bit */
90 outb_p(vol / 2, dev->io); /* Set the volume, high bit. */
91 outb_p(vol % 2, dev->io + 2); /* Set the volume, low bit. */
92 mutex_unlock(&dev->lock);
95 static int typhoon_setfreq_generic(struct typhoon *dev,
96 unsigned long frequency)
102 * The frequency transfer curve is not linear. The best fit I could
105 * outval = -155 + exp((f + 15.55) * 0.057))
107 * where frequency f is in MHz. Since we don't have exp in the kernel,
108 * I approximate this function by a third order polynomial.
112 mutex_lock(&dev->lock);
114 outval = (x * x + 2500) / 5000;
115 outval = (outval * x + 5000) / 10000;
116 outval -= (10 * x * x + 10433) / 20866;
117 outval += 4 * x - 11505;
119 outb_p((outval >> 8) & 0x01, dev->io + 4);
120 outb_p(outval >> 9, dev->io + 6);
121 outb_p(outval & 0xff, dev->io + 8);
122 mutex_unlock(&dev->lock);
127 static int typhoon_setfreq(struct typhoon *dev, unsigned long frequency)
129 typhoon_setfreq_generic(dev, frequency);
130 dev->curfreq = frequency;
134 static void typhoon_mute(struct typhoon *dev)
138 typhoon_setvol_generic(dev, 0);
139 typhoon_setfreq_generic(dev, dev->mutefreq);
143 static void typhoon_unmute(struct typhoon *dev)
147 typhoon_setfreq_generic(dev, dev->curfreq);
148 typhoon_setvol_generic(dev, dev->curvol);
152 static int typhoon_setvol(struct typhoon *dev, int vol)
154 if (dev->muted && vol != 0) { /* user is unmuting the card */
159 if (vol == dev->curvol) /* requested volume == current */
162 if (vol == 0) { /* volume == 0 means mute the card */
167 typhoon_setvol_generic(dev, vol);
172 static int vidioc_querycap(struct file *file, void *priv,
173 struct v4l2_capability *v)
175 strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
176 strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
177 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
178 v->version = RADIO_VERSION;
179 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
183 static int vidioc_g_tuner(struct file *file, void *priv,
184 struct v4l2_tuner *v)
189 strlcpy(v->name, "FM", sizeof(v->name));
190 v->type = V4L2_TUNER_RADIO;
191 v->rangelow = 87.5 * 16000;
192 v->rangehigh = 108 * 16000;
193 v->rxsubchans = V4L2_TUNER_SUB_MONO;
194 v->capability = V4L2_TUNER_CAP_LOW;
195 v->audmode = V4L2_TUNER_MODE_MONO;
196 v->signal = 0xFFFF; /* We can't get the signal strength */
200 static int vidioc_s_tuner(struct file *file, void *priv,
201 struct v4l2_tuner *v)
203 return v->index ? -EINVAL : 0;
206 static int vidioc_g_frequency(struct file *file, void *priv,
207 struct v4l2_frequency *f)
209 struct typhoon *dev = video_drvdata(file);
211 f->type = V4L2_TUNER_RADIO;
212 f->frequency = dev->curfreq;
216 static int vidioc_s_frequency(struct file *file, void *priv,
217 struct v4l2_frequency *f)
219 struct typhoon *dev = video_drvdata(file);
221 dev->curfreq = f->frequency;
222 typhoon_setfreq(dev, dev->curfreq);
226 static int vidioc_queryctrl(struct file *file, void *priv,
227 struct v4l2_queryctrl *qc)
230 case V4L2_CID_AUDIO_MUTE:
231 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
232 case V4L2_CID_AUDIO_VOLUME:
233 return v4l2_ctrl_query_fill(qc, 0, 65535, 16384, 65535);
238 static int vidioc_g_ctrl(struct file *file, void *priv,
239 struct v4l2_control *ctrl)
241 struct typhoon *dev = video_drvdata(file);
244 case V4L2_CID_AUDIO_MUTE:
245 ctrl->value = dev->muted;
247 case V4L2_CID_AUDIO_VOLUME:
248 ctrl->value = dev->curvol;
254 static int vidioc_s_ctrl (struct file *file, void *priv,
255 struct v4l2_control *ctrl)
257 struct typhoon *dev = video_drvdata(file);
260 case V4L2_CID_AUDIO_MUTE:
266 case V4L2_CID_AUDIO_VOLUME:
267 typhoon_setvol(dev, ctrl->value);
273 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
279 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
281 return i ? -EINVAL : 0;
284 static int vidioc_g_audio(struct file *file, void *priv,
285 struct v4l2_audio *a)
288 strlcpy(a->name, "Radio", sizeof(a->name));
289 a->capability = V4L2_AUDCAP_STEREO;
293 static int vidioc_s_audio(struct file *file, void *priv,
294 struct v4l2_audio *a)
296 return a->index ? -EINVAL : 0;
299 static int vidioc_log_status(struct file *file, void *priv)
301 struct typhoon *dev = video_drvdata(file);
302 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
304 v4l2_info(v4l2_dev, BANNER);
306 v4l2_info(v4l2_dev, "Load type: Driver loaded as a module\n\n");
308 v4l2_info(v4l2_dev, "Load type: Driver compiled into kernel\n\n");
310 v4l2_info(v4l2_dev, "frequency = %lu kHz\n", dev->curfreq >> 4);
311 v4l2_info(v4l2_dev, "volume = %d\n", dev->curvol);
312 v4l2_info(v4l2_dev, "mute = %s\n", dev->muted ? "on" : "off");
313 v4l2_info(v4l2_dev, "io = 0x%x\n", dev->io);
314 v4l2_info(v4l2_dev, "mute frequency = %lu kHz\n", dev->mutefreq >> 4);
318 static int typhoon_open(struct file *file)
323 static int typhoon_release(struct file *file)
328 static const struct v4l2_file_operations typhoon_fops = {
329 .owner = THIS_MODULE,
330 .open = typhoon_open,
331 .release = typhoon_release,
332 .ioctl = video_ioctl2,
335 static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
336 .vidioc_log_status = vidioc_log_status,
337 .vidioc_querycap = vidioc_querycap,
338 .vidioc_g_tuner = vidioc_g_tuner,
339 .vidioc_s_tuner = vidioc_s_tuner,
340 .vidioc_g_audio = vidioc_g_audio,
341 .vidioc_s_audio = vidioc_s_audio,
342 .vidioc_g_input = vidioc_g_input,
343 .vidioc_s_input = vidioc_s_input,
344 .vidioc_g_frequency = vidioc_g_frequency,
345 .vidioc_s_frequency = vidioc_s_frequency,
346 .vidioc_queryctrl = vidioc_queryctrl,
347 .vidioc_g_ctrl = vidioc_g_ctrl,
348 .vidioc_s_ctrl = vidioc_s_ctrl,
351 static int __init typhoon_init(void)
353 struct typhoon *dev = &typhoon_card;
354 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
357 strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name));
359 dev->curfreq = dev->mutefreq = mutefreq;
362 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n");
366 if (dev->mutefreq < 87000 || dev->mutefreq > 108500) {
367 v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n");
368 v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
372 mutex_init(&dev->lock);
373 if (!request_region(dev->io, 8, "typhoon")) {
374 v4l2_err(v4l2_dev, "port 0x%x already in use\n",
379 res = v4l2_device_register(NULL, v4l2_dev);
381 release_region(dev->io, 8);
382 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
385 v4l2_info(v4l2_dev, BANNER);
387 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
388 dev->vdev.v4l2_dev = v4l2_dev;
389 dev->vdev.fops = &typhoon_fops;
390 dev->vdev.ioctl_ops = &typhoon_ioctl_ops;
391 dev->vdev.release = video_device_release_empty;
392 video_set_drvdata(&dev->vdev, dev);
393 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
394 v4l2_device_unregister(&dev->v4l2_dev);
395 release_region(dev->io, 8);
398 v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io);
399 v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", dev->mutefreq);
402 /* mute card - prevents noisy bootups */
408 static void __exit typhoon_exit(void)
410 struct typhoon *dev = &typhoon_card;
412 video_unregister_device(&dev->vdev);
413 v4l2_device_unregister(&dev->v4l2_dev);
414 release_region(dev->io, 8);
417 module_init(typhoon_init);
418 module_exit(typhoon_exit);