blob: 398726abc0c863947bdf873bd4ff075880904fc4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Notes on the hardware
5 *
6 * This card has two output sockets, one for speakers and one for line.
7 * The speaker output has volume control, but only in four discrete
8 * steps. The line output has neither volume control nor mute.
9 *
10 * The card has auto-stereo according to its manual, although it all
11 * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
12 * antenna - I really don't know for sure.
13 *
14 * Frequency control is done digitally.
15 *
16 * Volume control is done digitally, but there are only four different
17 * possible values. So you should better always turn the volume up and
18 * use line control. I got the best results by connecting line output
19 * to the sound card microphone input. For such a configuration the
20 * volume control has no effect, since volume control only influences
21 * the speaker output.
22 *
23 * There is no explicit mute/unmute. So I set the radio frequency to a
24 * value where I do expect just noise and turn the speaker volume down.
25 * The frequency change is necessary since the card never seems to be
26 * completely silent.
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030027 *
28 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/module.h> /* Modules */
32#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070033#include <linux/ioport.h> /* request_region */
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030034#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuilf1bfe892009-03-06 13:54:52 -030035#include <linux/io.h> /* outb, outb_p */
Hans Verkuilf1bfe892009-03-06 13:54:52 -030036#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030039#define DRIVER_VERSION "0.1.2"
40
Hans Verkuilf1bfe892009-03-06 13:54:52 -030041MODULE_AUTHOR("Dr. Henrik Seidel");
42MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
43MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030044MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#ifndef CONFIG_RADIO_TYPHOON_PORT
47#define CONFIG_RADIO_TYPHOON_PORT -1
48#endif
49
50#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
51#define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
52#endif
53
Hans Verkuilf1bfe892009-03-06 13:54:52 -030054static int io = CONFIG_RADIO_TYPHOON_PORT;
55static int radio_nr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Hans Verkuilf1bfe892009-03-06 13:54:52 -030057module_param(io, int, 0);
58MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
59
60module_param(radio_nr, int, 0);
61
62static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
63module_param(mutefreq, ulong, 0);
64MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
65
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030066#define BANNER "Typhoon Radio Card driver v" DRIVER_VERSION "\n"
Hans Verkuilf1bfe892009-03-06 13:54:52 -030067
68struct typhoon {
69 struct v4l2_device v4l2_dev;
70 struct video_device vdev;
71 int io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int curvol;
73 int muted;
74 unsigned long curfreq;
75 unsigned long mutefreq;
Ingo Molnar3593cab2006-02-07 06:49:14 -020076 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Hans Verkuilf1bfe892009-03-06 13:54:52 -030079static struct typhoon typhoon_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Hans Verkuilf1bfe892009-03-06 13:54:52 -030081static void typhoon_setvol_generic(struct typhoon *dev, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Ingo Molnar3593cab2006-02-07 06:49:14 -020083 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 vol >>= 14; /* Map 16 bit to 2 bit */
85 vol &= 3;
Hans Verkuilf1bfe892009-03-06 13:54:52 -030086 outb_p(vol / 2, dev->io); /* Set the volume, high bit. */
87 outb_p(vol % 2, dev->io + 2); /* Set the volume, low bit. */
Ingo Molnar3593cab2006-02-07 06:49:14 -020088 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Hans Verkuilf1bfe892009-03-06 13:54:52 -030091static int typhoon_setfreq_generic(struct typhoon *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned long frequency)
93{
94 unsigned long outval;
95 unsigned long x;
96
97 /*
98 * The frequency transfer curve is not linear. The best fit I could
99 * get is
100 *
101 * outval = -155 + exp((f + 15.55) * 0.057))
102 *
103 * where frequency f is in MHz. Since we don't have exp in the kernel,
104 * I approximate this function by a third order polynomial.
105 *
106 */
107
Ingo Molnar3593cab2006-02-07 06:49:14 -0200108 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 x = frequency / 160;
110 outval = (x * x + 2500) / 5000;
111 outval = (outval * x + 5000) / 10000;
112 outval -= (10 * x * x + 10433) / 20866;
113 outval += 4 * x - 11505;
114
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300115 outb_p((outval >> 8) & 0x01, dev->io + 4);
116 outb_p(outval >> 9, dev->io + 6);
117 outb_p(outval & 0xff, dev->io + 8);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200118 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return 0;
121}
122
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300123static int typhoon_setfreq(struct typhoon *dev, unsigned long frequency)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 typhoon_setfreq_generic(dev, frequency);
126 dev->curfreq = frequency;
127 return 0;
128}
129
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300130static void typhoon_mute(struct typhoon *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 if (dev->muted == 1)
133 return;
134 typhoon_setvol_generic(dev, 0);
135 typhoon_setfreq_generic(dev, dev->mutefreq);
136 dev->muted = 1;
137}
138
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300139static void typhoon_unmute(struct typhoon *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 if (dev->muted == 0)
142 return;
143 typhoon_setfreq_generic(dev, dev->curfreq);
144 typhoon_setvol_generic(dev, dev->curvol);
145 dev->muted = 0;
146}
147
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300148static int typhoon_setvol(struct typhoon *dev, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 if (dev->muted && vol != 0) { /* user is unmuting the card */
151 dev->curvol = vol;
152 typhoon_unmute(dev);
153 return 0;
154 }
155 if (vol == dev->curvol) /* requested volume == current */
156 return 0;
157
158 if (vol == 0) { /* volume == 0 means mute the card */
159 typhoon_mute(dev);
160 dev->curvol = vol;
161 return 0;
162 }
163 typhoon_setvol_generic(dev, vol);
164 dev->curvol = vol;
165 return 0;
166}
167
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300168static int vidioc_querycap(struct file *file, void *priv,
169 struct v4l2_capability *v)
170{
171 strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
172 strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300173 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300174 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300175 return 0;
176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300178static int vidioc_g_tuner(struct file *file, void *priv,
179 struct v4l2_tuner *v)
180{
181 if (v->index > 0)
182 return -EINVAL;
183
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300184 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300185 v->type = V4L2_TUNER_RADIO;
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300186 v->rangelow = 87.5 * 16000;
187 v->rangehigh = 108 * 16000;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300188 v->rxsubchans = V4L2_TUNER_SUB_MONO;
189 v->capability = V4L2_TUNER_CAP_LOW;
190 v->audmode = V4L2_TUNER_MODE_MONO;
191 v->signal = 0xFFFF; /* We can't get the signal strength */
192 return 0;
193}
194
195static int vidioc_s_tuner(struct file *file, void *priv,
196 struct v4l2_tuner *v)
197{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300198 return v->index ? -EINVAL : 0;
199}
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300200
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300201static int vidioc_g_frequency(struct file *file, void *priv,
202 struct v4l2_frequency *f)
203{
204 struct typhoon *dev = video_drvdata(file);
205
Hans Verkuila3a9e282009-11-27 04:33:25 -0300206 if (f->tuner != 0)
207 return -EINVAL;
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300208 f->type = V4L2_TUNER_RADIO;
209 f->frequency = dev->curfreq;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300210 return 0;
211}
212
213static int vidioc_s_frequency(struct file *file, void *priv,
214 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300216 struct typhoon *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Hans Verkuila3a9e282009-11-27 04:33:25 -0300218 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
219 return -EINVAL;
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300220 dev->curfreq = f->frequency;
221 typhoon_setfreq(dev, dev->curfreq);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300222 return 0;
223}
224
225static int vidioc_queryctrl(struct file *file, void *priv,
226 struct v4l2_queryctrl *qc)
227{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300228 switch (qc->id) {
229 case V4L2_CID_AUDIO_MUTE:
230 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
231 case V4L2_CID_AUDIO_VOLUME:
232 return v4l2_ctrl_query_fill(qc, 0, 65535, 16384, 65535);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300233 }
234 return -EINVAL;
235}
236
237static int vidioc_g_ctrl(struct file *file, void *priv,
238 struct v4l2_control *ctrl)
239{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300240 struct typhoon *dev = video_drvdata(file);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300241
242 switch (ctrl->id) {
243 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300244 ctrl->value = dev->muted;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300245 return 0;
246 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300247 ctrl->value = dev->curvol;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300248 return 0;
249 }
250 return -EINVAL;
251}
252
253static int vidioc_s_ctrl (struct file *file, void *priv,
254 struct v4l2_control *ctrl)
255{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300256 struct typhoon *dev = video_drvdata(file);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300257
258 switch (ctrl->id) {
259 case V4L2_CID_AUDIO_MUTE:
260 if (ctrl->value)
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300261 typhoon_mute(dev);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300262 else
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300263 typhoon_unmute(dev);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300264 return 0;
265 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300266 typhoon_setvol(dev, ctrl->value);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300267 return 0;
268 }
269 return -EINVAL;
270}
271
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300272static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
273{
274 *i = 0;
275 return 0;
276}
277
278static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
279{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300280 return i ? -EINVAL : 0;
281}
282
283static int vidioc_g_audio(struct file *file, void *priv,
284 struct v4l2_audio *a)
285{
286 a->index = 0;
287 strlcpy(a->name, "Radio", sizeof(a->name));
288 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300289 return 0;
290}
291
292static int vidioc_s_audio(struct file *file, void *priv,
293 struct v4l2_audio *a)
294{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300295 return a->index ? -EINVAL : 0;
296}
297
298static int vidioc_log_status(struct file *file, void *priv)
299{
300 struct typhoon *dev = video_drvdata(file);
301 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
302
303 v4l2_info(v4l2_dev, BANNER);
304#ifdef MODULE
305 v4l2_info(v4l2_dev, "Load type: Driver loaded as a module\n\n");
306#else
307 v4l2_info(v4l2_dev, "Load type: Driver compiled into kernel\n\n");
308#endif
309 v4l2_info(v4l2_dev, "frequency = %lu kHz\n", dev->curfreq >> 4);
310 v4l2_info(v4l2_dev, "volume = %d\n", dev->curvol);
311 v4l2_info(v4l2_dev, "mute = %s\n", dev->muted ? "on" : "off");
312 v4l2_info(v4l2_dev, "io = 0x%x\n", dev->io);
313 v4l2_info(v4l2_dev, "mute frequency = %lu kHz\n", dev->mutefreq >> 4);
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Hans Verkuilbec43662008-12-30 06:58:20 -0300317static const struct v4l2_file_operations typhoon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .owner = THIS_MODULE,
Hans Verkuild2c998f2010-11-14 09:49:34 -0300319 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Hans Verkuila3998102008-07-21 02:57:38 -0300322static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300323 .vidioc_log_status = vidioc_log_status,
Douglas Landgraf3f6892a2007-04-24 08:40:06 -0300324 .vidioc_querycap = vidioc_querycap,
325 .vidioc_g_tuner = vidioc_g_tuner,
326 .vidioc_s_tuner = vidioc_s_tuner,
327 .vidioc_g_audio = vidioc_g_audio,
328 .vidioc_s_audio = vidioc_s_audio,
329 .vidioc_g_input = vidioc_g_input,
330 .vidioc_s_input = vidioc_s_input,
331 .vidioc_g_frequency = vidioc_g_frequency,
332 .vidioc_s_frequency = vidioc_s_frequency,
333 .vidioc_queryctrl = vidioc_queryctrl,
334 .vidioc_g_ctrl = vidioc_g_ctrl,
335 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336};
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int __init typhoon_init(void)
339{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300340 struct typhoon *dev = &typhoon_card;
341 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
342 int res;
343
344 strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name));
345 dev->io = io;
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300346
347 if (dev->io == -1) {
348 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hans Verkuild2c998f2010-11-14 09:49:34 -0300352 if (mutefreq < 87000 || mutefreq > 108500) {
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300353 v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n");
354 v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EINVAL;
356 }
Hans Verkuild2c998f2010-11-14 09:49:34 -0300357 dev->curfreq = dev->mutefreq = mutefreq << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300359 mutex_init(&dev->lock);
360 if (!request_region(dev->io, 8, "typhoon")) {
361 v4l2_err(v4l2_dev, "port 0x%x already in use\n",
362 dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -EBUSY;
364 }
365
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300366 res = v4l2_device_register(NULL, v4l2_dev);
367 if (res < 0) {
368 release_region(dev->io, 8);
369 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
370 return res;
371 }
372 v4l2_info(v4l2_dev, BANNER);
373
374 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
375 dev->vdev.v4l2_dev = v4l2_dev;
376 dev->vdev.fops = &typhoon_fops;
377 dev->vdev.ioctl_ops = &typhoon_ioctl_ops;
378 dev->vdev.release = video_device_release_empty;
379 video_set_drvdata(&dev->vdev, dev);
Hans Verkuild2c998f2010-11-14 09:49:34 -0300380
381 /* mute card - prevents noisy bootups */
382 typhoon_mute(dev);
383
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300384 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
385 v4l2_device_unregister(&dev->v4l2_dev);
386 release_region(dev->io, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -EINVAL;
388 }
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300389 v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io);
Hans Verkuild2c998f2010-11-14 09:49:34 -0300390 v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", mutefreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return 0;
393}
394
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300395static void __exit typhoon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300397 struct typhoon *dev = &typhoon_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300399 video_unregister_device(&dev->vdev);
400 v4l2_device_unregister(&dev->v4l2_dev);
401 release_region(dev->io, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404module_init(typhoon_init);
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300405module_exit(typhoon_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406