nfs: Panic when commit fails
[linux-2.6/verdex.git] / drivers / media / radio / radio-typhoon.c
blob3a98f139949565e23c84ce6ba7cd715b79d36e39
1 /* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
4 * Card manufacturer:
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
24 * the speaker output.
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.
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 <media/v4l2-device.h>
41 #include <media/v4l2-ioctl.h>
43 MODULE_AUTHOR("Dr. Henrik Seidel");
44 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
45 MODULE_LICENSE("GPL");
47 #ifndef CONFIG_RADIO_TYPHOON_PORT
48 #define CONFIG_RADIO_TYPHOON_PORT -1
49 #endif
51 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
52 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
53 #endif
55 static int io = CONFIG_RADIO_TYPHOON_PORT;
56 static int radio_nr = -1;
58 module_param(io, int, 0);
59 MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
61 module_param(radio_nr, int, 0);
63 static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
64 module_param(mutefreq, ulong, 0);
65 MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
67 #define RADIO_VERSION KERNEL_VERSION(0, 1, 1)
69 #define BANNER "Typhoon Radio Card driver v0.1.1\n"
71 struct typhoon {
72 struct v4l2_device v4l2_dev;
73 struct video_device vdev;
74 int io;
75 int curvol;
76 int muted;
77 unsigned long curfreq;
78 unsigned long mutefreq;
79 struct mutex lock;
82 static struct typhoon typhoon_card;
84 static void typhoon_setvol_generic(struct typhoon *dev, int vol)
86 mutex_lock(&dev->lock);
87 vol >>= 14; /* Map 16 bit to 2 bit */
88 vol &= 3;
89 outb_p(vol / 2, dev->io); /* Set the volume, high bit. */
90 outb_p(vol % 2, dev->io + 2); /* Set the volume, low bit. */
91 mutex_unlock(&dev->lock);
94 static int typhoon_setfreq_generic(struct typhoon *dev,
95 unsigned long frequency)
97 unsigned long outval;
98 unsigned long x;
101 * The frequency transfer curve is not linear. The best fit I could
102 * get is
104 * outval = -155 + exp((f + 15.55) * 0.057))
106 * where frequency f is in MHz. Since we don't have exp in the kernel,
107 * I approximate this function by a third order polynomial.
111 mutex_lock(&dev->lock);
112 x = frequency / 160;
113 outval = (x * x + 2500) / 5000;
114 outval = (outval * x + 5000) / 10000;
115 outval -= (10 * x * x + 10433) / 20866;
116 outval += 4 * x - 11505;
118 outb_p((outval >> 8) & 0x01, dev->io + 4);
119 outb_p(outval >> 9, dev->io + 6);
120 outb_p(outval & 0xff, dev->io + 8);
121 mutex_unlock(&dev->lock);
123 return 0;
126 static int typhoon_setfreq(struct typhoon *dev, unsigned long frequency)
128 typhoon_setfreq_generic(dev, frequency);
129 dev->curfreq = frequency;
130 return 0;
133 static void typhoon_mute(struct typhoon *dev)
135 if (dev->muted == 1)
136 return;
137 typhoon_setvol_generic(dev, 0);
138 typhoon_setfreq_generic(dev, dev->mutefreq);
139 dev->muted = 1;
142 static void typhoon_unmute(struct typhoon *dev)
144 if (dev->muted == 0)
145 return;
146 typhoon_setfreq_generic(dev, dev->curfreq);
147 typhoon_setvol_generic(dev, dev->curvol);
148 dev->muted = 0;
151 static int typhoon_setvol(struct typhoon *dev, int vol)
153 if (dev->muted && vol != 0) { /* user is unmuting the card */
154 dev->curvol = vol;
155 typhoon_unmute(dev);
156 return 0;
158 if (vol == dev->curvol) /* requested volume == current */
159 return 0;
161 if (vol == 0) { /* volume == 0 means mute the card */
162 typhoon_mute(dev);
163 dev->curvol = vol;
164 return 0;
166 typhoon_setvol_generic(dev, vol);
167 dev->curvol = vol;
168 return 0;
171 static int vidioc_querycap(struct file *file, void *priv,
172 struct v4l2_capability *v)
174 strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
175 strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
176 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
177 v->version = RADIO_VERSION;
178 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
179 return 0;
182 static int vidioc_g_tuner(struct file *file, void *priv,
183 struct v4l2_tuner *v)
185 if (v->index > 0)
186 return -EINVAL;
188 strlcpy(v->name, "FM", sizeof(v->name));
189 v->type = V4L2_TUNER_RADIO;
190 v->rangelow = 87.5 * 16000;
191 v->rangehigh = 108 * 16000;
192 v->rxsubchans = V4L2_TUNER_SUB_MONO;
193 v->capability = V4L2_TUNER_CAP_LOW;
194 v->audmode = V4L2_TUNER_MODE_MONO;
195 v->signal = 0xFFFF; /* We can't get the signal strength */
196 return 0;
199 static int vidioc_s_tuner(struct file *file, void *priv,
200 struct v4l2_tuner *v)
202 return v->index ? -EINVAL : 0;
205 static int vidioc_g_frequency(struct file *file, void *priv,
206 struct v4l2_frequency *f)
208 struct typhoon *dev = video_drvdata(file);
210 f->type = V4L2_TUNER_RADIO;
211 f->frequency = dev->curfreq;
212 return 0;
215 static int vidioc_s_frequency(struct file *file, void *priv,
216 struct v4l2_frequency *f)
218 struct typhoon *dev = video_drvdata(file);
220 dev->curfreq = f->frequency;
221 typhoon_setfreq(dev, dev->curfreq);
222 return 0;
225 static int vidioc_queryctrl(struct file *file, void *priv,
226 struct v4l2_queryctrl *qc)
228 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);
234 return -EINVAL;
237 static int vidioc_g_ctrl(struct file *file, void *priv,
238 struct v4l2_control *ctrl)
240 struct typhoon *dev = video_drvdata(file);
242 switch (ctrl->id) {
243 case V4L2_CID_AUDIO_MUTE:
244 ctrl->value = dev->muted;
245 return 0;
246 case V4L2_CID_AUDIO_VOLUME:
247 ctrl->value = dev->curvol;
248 return 0;
250 return -EINVAL;
253 static int vidioc_s_ctrl (struct file *file, void *priv,
254 struct v4l2_control *ctrl)
256 struct typhoon *dev = video_drvdata(file);
258 switch (ctrl->id) {
259 case V4L2_CID_AUDIO_MUTE:
260 if (ctrl->value)
261 typhoon_mute(dev);
262 else
263 typhoon_unmute(dev);
264 return 0;
265 case V4L2_CID_AUDIO_VOLUME:
266 typhoon_setvol(dev, ctrl->value);
267 return 0;
269 return -EINVAL;
272 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
274 *i = 0;
275 return 0;
278 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
280 return i ? -EINVAL : 0;
283 static int vidioc_g_audio(struct file *file, void *priv,
284 struct v4l2_audio *a)
286 a->index = 0;
287 strlcpy(a->name, "Radio", sizeof(a->name));
288 a->capability = V4L2_AUDCAP_STEREO;
289 return 0;
292 static int vidioc_s_audio(struct file *file, void *priv,
293 struct v4l2_audio *a)
295 return a->index ? -EINVAL : 0;
298 static int vidioc_log_status(struct file *file, void *priv)
300 struct typhoon *dev = video_drvdata(file);
301 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
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);
314 return 0;
317 static const struct v4l2_file_operations typhoon_fops = {
318 .owner = THIS_MODULE,
319 .ioctl = video_ioctl2,
322 static const struct v4l2_ioctl_ops typhoon_ioctl_ops = {
323 .vidioc_log_status = vidioc_log_status,
324 .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,
338 static int __init typhoon_init(void)
340 struct typhoon *dev = &typhoon_card;
341 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
342 int res;
344 strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name));
345 dev->io = io;
346 dev->curfreq = dev->mutefreq = mutefreq;
348 if (dev->io == -1) {
349 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n");
350 return -EINVAL;
353 if (dev->mutefreq < 87000 || dev->mutefreq > 108500) {
354 v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n");
355 v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
356 return -EINVAL;
359 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);
363 return -EBUSY;
366 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;
372 v4l2_info(v4l2_dev, BANNER);
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);
380 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
381 v4l2_device_unregister(&dev->v4l2_dev);
382 release_region(dev->io, 8);
383 return -EINVAL;
385 v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io);
386 v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", dev->mutefreq);
387 dev->mutefreq <<= 4;
389 /* mute card - prevents noisy bootups */
390 typhoon_mute(dev);
392 return 0;
395 static void __exit typhoon_exit(void)
397 struct typhoon *dev = &typhoon_card;
399 video_unregister_device(&dev->vdev);
400 v4l2_device_unregister(&dev->v4l2_dev);
401 release_region(dev->io, 8);
404 module_init(typhoon_init);
405 module_exit(typhoon_exit);