blob: 5c3b319dab37cadaadc64c576bc18482e943d444 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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.
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030030 *
31 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
34#include <linux/module.h> /* Modules */
35#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070036#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/proc_fs.h> /* radio card status report */
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -030038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h> /* outb, outb_p */
40#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030041#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030042#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030043#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030045#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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 {
Hans Verkuil3ca685a2008-08-23 04:49:13 -030082 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int iobase;
84 int curvol;
85 int muted;
86 unsigned long curfreq;
87 unsigned long mutefreq;
Ingo Molnar3593cab2006-02-07 06:49:14 -020088 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
100{
Ingo Molnar3593cab2006-02-07 06:49:14 -0200101 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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. */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200106 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
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
Ingo Molnar3593cab2006-02-07 06:49:14 -0200126 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200136 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
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
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300186static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300197static 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}
213
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300226 struct typhoon_device *typhoon = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300228 typhoon->curfreq = f->frequency;
229 typhoon_setfreq(typhoon, typhoon->curfreq);
230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300233static int vidioc_g_frequency(struct file *file, void *priv,
234 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300236 struct typhoon_device *typhoon = video_drvdata(file);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300237
238 f->type = V4L2_TUNER_RADIO;
239 f->frequency = typhoon->curfreq;
240
241 return 0;
242}
243
244static int vidioc_queryctrl(struct file *file, void *priv,
245 struct v4l2_queryctrl *qc)
246{
247 int i;
248
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]),
252 sizeof(*qc));
253 return 0;
254 }
255 }
256 return -EINVAL;
257}
258
259static int vidioc_g_ctrl(struct file *file, void *priv,
260 struct v4l2_control *ctrl)
261{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300262 struct typhoon_device *typhoon = video_drvdata(file);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300263
264 switch (ctrl->id) {
265 case V4L2_CID_AUDIO_MUTE:
266 ctrl->value = typhoon->muted;
267 return 0;
268 case V4L2_CID_AUDIO_VOLUME:
269 ctrl->value = typhoon->curvol;
270 return 0;
271 }
272 return -EINVAL;
273}
274
275static int vidioc_s_ctrl (struct file *file, void *priv,
276 struct v4l2_control *ctrl)
277{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300278 struct typhoon_device *typhoon = video_drvdata(file);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300279
280 switch (ctrl->id) {
281 case V4L2_CID_AUDIO_MUTE:
282 if (ctrl->value)
283 typhoon_mute(typhoon);
284 else
285 typhoon_unmute(typhoon);
286 return 0;
287 case V4L2_CID_AUDIO_VOLUME:
288 typhoon_setvol(typhoon, ctrl->value);
289 return 0;
290 }
291 return -EINVAL;
292}
293
294static int vidioc_g_audio(struct file *file, void *priv,
295 struct v4l2_audio *a)
296{
297 if (a->index > 1)
298 return -EINVAL;
299
300 strcpy(a->name, "Radio");
301 a->capability = V4L2_AUDCAP_STEREO;
302 return 0;
303}
304
305static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
306{
307 *i = 0;
308 return 0;
309}
310
311static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
312{
313 if (i != 0)
314 return -EINVAL;
315 return 0;
316}
317
318static int vidioc_s_audio(struct file *file, void *priv,
319 struct v4l2_audio *a)
320{
321 if (a->index != 0)
322 return -EINVAL;
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326static struct typhoon_device typhoon_unit =
327{
328 .iobase = CONFIG_RADIO_TYPHOON_PORT,
329 .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
330 .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
331};
332
Hans Verkuilbec43662008-12-30 06:58:20 -0300333static int typhoon_exclusive_open(struct file *file)
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300334{
335 return test_and_set_bit(0, &typhoon_unit.in_use) ? -EBUSY : 0;
336}
337
Hans Verkuilbec43662008-12-30 06:58:20 -0300338static int typhoon_exclusive_release(struct file *file)
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300339{
340 clear_bit(0, &typhoon_unit.in_use);
341 return 0;
342}
343
Hans Verkuilbec43662008-12-30 06:58:20 -0300344static const struct v4l2_file_operations typhoon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300346 .open = typhoon_exclusive_open,
347 .release = typhoon_exclusive_release,
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300348 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Hans Verkuila3998102008-07-21 02:57:38 -0300351static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300352 .vidioc_querycap = vidioc_querycap,
353 .vidioc_g_tuner = vidioc_g_tuner,
354 .vidioc_s_tuner = vidioc_s_tuner,
355 .vidioc_g_audio = vidioc_g_audio,
356 .vidioc_s_audio = vidioc_s_audio,
357 .vidioc_g_input = vidioc_g_input,
358 .vidioc_s_input = vidioc_s_input,
359 .vidioc_g_frequency = vidioc_g_frequency,
360 .vidioc_s_frequency = vidioc_s_frequency,
361 .vidioc_queryctrl = vidioc_queryctrl,
362 .vidioc_g_ctrl = vidioc_g_ctrl,
363 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
Hans Verkuila3998102008-07-21 02:57:38 -0300366static struct video_device typhoon_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300367 .name = "Typhoon Radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300368 .fops = &typhoon_fops,
369 .ioctl_ops = &typhoon_ioctl_ops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300370 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -0300371};
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
374
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300375static int typhoon_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 #ifdef MODULE
378 #define MODULEPROCSTRING "Driver loaded as a module"
379 #else
380 #define MODULEPROCSTRING "Driver compiled into kernel"
381 #endif
382
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300383 seq_puts(m, BANNER);
384 seq_puts(m, "Load type: " MODULEPROCSTRING "\n\n");
385 seq_printf(m, "frequency = %lu kHz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 typhoon_unit.curfreq >> 4);
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300387 seq_printf(m, "volume = %d\n", typhoon_unit.curvol);
388 seq_printf(m, "mute = %s\n", typhoon_unit.muted ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 "on" : "off");
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300390 seq_printf(m, "iobase = 0x%x\n", typhoon_unit.iobase);
391 seq_printf(m, "mute frequency = %lu kHz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 typhoon_unit.mutefreq >> 4);
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300396static int typhoon_proc_open(struct inode *inode, struct file *file)
397{
398 return single_open(file, typhoon_proc_show, NULL);
399}
400
401static const struct file_operations typhoon_proc_fops = {
402 .owner = THIS_MODULE,
403 .open = typhoon_proc_open,
404 .read = seq_read,
405 .llseek = seq_lseek,
406 .release = single_release,
407};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
409
410MODULE_AUTHOR("Dr. Henrik Seidel");
411MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
412MODULE_LICENSE("GPL");
413
414static int io = -1;
415static int radio_nr = -1;
416
417module_param(io, int, 0);
418MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
419module_param(radio_nr, int, 0);
420
421#ifdef MODULE
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300422static unsigned long mutefreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423module_param(mutefreq, ulong, 0);
424MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
425#endif
426
427static int __init typhoon_init(void)
428{
429#ifdef MODULE
430 if (io == -1) {
431 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
432 return -EINVAL;
433 }
434 typhoon_unit.iobase = io;
435
436 if (mutefreq < 87000 || mutefreq > 108500) {
437 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
438 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
439 return -EINVAL;
440 }
441 typhoon_unit.mutefreq = mutefreq;
442#endif /* MODULE */
443
444 printk(KERN_INFO BANNER);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200445 mutex_init(&typhoon_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 io = typhoon_unit.iobase;
447 if (!request_region(io, 8, "typhoon")) {
448 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
449 typhoon_unit.iobase);
450 return -EBUSY;
451 }
452
Hans Verkuil601e9442008-08-23 07:24:07 -0300453 video_set_drvdata(&typhoon_radio, &typhoon_unit);
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300454 if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 release_region(io, 8);
456 return -EINVAL;
457 }
458 printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
459 printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
460 typhoon_unit.mutefreq);
461 typhoon_unit.mutefreq <<= 4;
462
463 /* mute card - prevents noisy bootups */
464 typhoon_mute(&typhoon_unit);
465
466#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
Alexey Dobriyan0b9c2b72008-04-10 21:34:39 -0300467 if (!proc_create("driver/radio-typhoon", 0, NULL, &typhoon_proc_fops))
Trent Piepho657de3c2006-06-20 00:30:57 -0300468 printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#endif
470
471 return 0;
472}
473
474static void __exit typhoon_cleanup_module(void)
475{
476
477#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
478 remove_proc_entry("driver/radio-typhoon", NULL);
479#endif
480
481 video_unregister_device(&typhoon_radio);
482 release_region(io, 8);
483}
484
485module_init(typhoon_init);
486module_exit(typhoon_cleanup_module);
487