Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6] / drivers / media / radio / radio-typhoon.c
1 /* 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.
30  *
31  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
32  */
33
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
44 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
45 #define RADIO_VERSION KERNEL_VERSION(0,1,1)
46 #define BANNER "Typhoon Radio Card driver v0.1.1\n"
47
48 static struct v4l2_queryctrl radio_qctrl[] = {
49         {
50                 .id            = V4L2_CID_AUDIO_MUTE,
51                 .name          = "Mute",
52                 .minimum       = 0,
53                 .maximum       = 1,
54                 .default_value = 1,
55                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
56         },{
57                 .id            = V4L2_CID_AUDIO_VOLUME,
58                 .name          = "Volume",
59                 .minimum       = 0,
60                 .maximum       = 65535,
61                 .step          = 1<<14,
62                 .default_value = 0xff,
63                 .type          = V4L2_CTRL_TYPE_INTEGER,
64         }
65 };
66
67
68 #ifndef CONFIG_RADIO_TYPHOON_PORT
69 #define CONFIG_RADIO_TYPHOON_PORT -1
70 #endif
71
72 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
73 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
74 #endif
75
76 #ifndef CONFIG_PROC_FS
77 #undef CONFIG_RADIO_TYPHOON_PROC_FS
78 #endif
79
80 struct typhoon_device {
81         int users;
82         int iobase;
83         int curvol;
84         int muted;
85         unsigned long curfreq;
86         unsigned long mutefreq;
87         struct mutex lock;
88 };
89
90 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
91 static int typhoon_setfreq_generic(struct typhoon_device *dev,
92                                    unsigned long frequency);
93 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
94 static void typhoon_mute(struct typhoon_device *dev);
95 static void typhoon_unmute(struct typhoon_device *dev);
96 static int typhoon_setvol(struct typhoon_device *dev, int vol);
97
98 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
99 {
100         mutex_lock(&dev->lock);
101         vol >>= 14;                             /* Map 16 bit to 2 bit */
102         vol &= 3;
103         outb_p(vol / 2, dev->iobase);           /* Set the volume, high bit. */
104         outb_p(vol % 2, dev->iobase + 2);       /* Set the volume, low bit. */
105         mutex_unlock(&dev->lock);
106 }
107
108 static int typhoon_setfreq_generic(struct typhoon_device *dev,
109                                    unsigned long frequency)
110 {
111         unsigned long outval;
112         unsigned long x;
113
114         /*
115          * The frequency transfer curve is not linear. The best fit I could
116          * get is
117          *
118          * outval = -155 + exp((f + 15.55) * 0.057))
119          *
120          * where frequency f is in MHz. Since we don't have exp in the kernel,
121          * I approximate this function by a third order polynomial.
122          *
123          */
124
125         mutex_lock(&dev->lock);
126         x = frequency / 160;
127         outval = (x * x + 2500) / 5000;
128         outval = (outval * x + 5000) / 10000;
129         outval -= (10 * x * x + 10433) / 20866;
130         outval += 4 * x - 11505;
131
132         outb_p((outval >> 8) & 0x01, dev->iobase + 4);
133         outb_p(outval >> 9, dev->iobase + 6);
134         outb_p(outval & 0xff, dev->iobase + 8);
135         mutex_unlock(&dev->lock);
136
137         return 0;
138 }
139
140 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
141 {
142         typhoon_setfreq_generic(dev, frequency);
143         dev->curfreq = frequency;
144         return 0;
145 }
146
147 static void typhoon_mute(struct typhoon_device *dev)
148 {
149         if (dev->muted == 1)
150                 return;
151         typhoon_setvol_generic(dev, 0);
152         typhoon_setfreq_generic(dev, dev->mutefreq);
153         dev->muted = 1;
154 }
155
156 static void typhoon_unmute(struct typhoon_device *dev)
157 {
158         if (dev->muted == 0)
159                 return;
160         typhoon_setfreq_generic(dev, dev->curfreq);
161         typhoon_setvol_generic(dev, dev->curvol);
162         dev->muted = 0;
163 }
164
165 static int typhoon_setvol(struct typhoon_device *dev, int vol)
166 {
167         if (dev->muted && vol != 0) {   /* user is unmuting the card */
168                 dev->curvol = vol;
169                 typhoon_unmute(dev);
170                 return 0;
171         }
172         if (vol == dev->curvol)         /* requested volume == current */
173                 return 0;
174
175         if (vol == 0) {                 /* volume == 0 means mute the card */
176                 typhoon_mute(dev);
177                 dev->curvol = vol;
178                 return 0;
179         }
180         typhoon_setvol_generic(dev, vol);
181         dev->curvol = vol;
182         return 0;
183 }
184
185 static int vidioc_querycap(struct file *file, void  *priv,
186                                         struct v4l2_capability *v)
187 {
188         strlcpy(v->driver, "radio-typhoon", sizeof(v->driver));
189         strlcpy(v->card, "Typhoon Radio", sizeof(v->card));
190         sprintf(v->bus_info, "ISA");
191         v->version = RADIO_VERSION;
192         v->capabilities = V4L2_CAP_TUNER;
193         return 0;
194 }
195
196 static int vidioc_g_tuner(struct file *file, void *priv,
197                                         struct v4l2_tuner *v)
198 {
199         if (v->index > 0)
200                 return -EINVAL;
201
202         strcpy(v->name, "FM");
203         v->type = V4L2_TUNER_RADIO;
204         v->rangelow = (87.5*16000);
205         v->rangehigh = (108*16000);
206         v->rxsubchans = V4L2_TUNER_SUB_MONO;
207         v->capability = V4L2_TUNER_CAP_LOW;
208         v->audmode = V4L2_TUNER_MODE_MONO;
209         v->signal = 0xFFFF;     /* We can't get the signal strength */
210         return 0;
211 }
212
213 static int vidioc_s_tuner(struct file *file, void *priv,
214                                         struct v4l2_tuner *v)
215 {
216         if (v->index > 0)
217                 return -EINVAL;
218
219         return 0;
220 }
221
222 static int vidioc_s_frequency(struct file *file, void *priv,
223                                         struct v4l2_frequency *f)
224 {
225         struct video_device *dev = video_devdata(file);
226         struct typhoon_device *typhoon = dev->priv;
227
228         typhoon->curfreq = f->frequency;
229         typhoon_setfreq(typhoon, typhoon->curfreq);
230         return 0;
231 }
232
233 static int vidioc_g_frequency(struct file *file, void *priv,
234                                         struct v4l2_frequency *f)
235 {
236         struct video_device *dev = video_devdata(file);
237         struct typhoon_device *typhoon = dev->priv;
238
239         f->type = V4L2_TUNER_RADIO;
240         f->frequency = typhoon->curfreq;
241
242         return 0;
243 }
244
245 static int vidioc_queryctrl(struct file *file, void *priv,
246                                         struct v4l2_queryctrl *qc)
247 {
248         int i;
249
250         for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
251                 if (qc->id && qc->id == radio_qctrl[i].id) {
252                         memcpy(qc, &(radio_qctrl[i]),
253                                                 sizeof(*qc));
254                         return 0;
255                 }
256         }
257         return -EINVAL;
258 }
259
260 static int vidioc_g_ctrl(struct file *file, void *priv,
261                                         struct v4l2_control *ctrl)
262 {
263         struct video_device *dev = video_devdata(file);
264         struct typhoon_device *typhoon = dev->priv;
265
266         switch (ctrl->id) {
267         case V4L2_CID_AUDIO_MUTE:
268                 ctrl->value = typhoon->muted;
269                 return 0;
270         case V4L2_CID_AUDIO_VOLUME:
271                 ctrl->value = typhoon->curvol;
272                 return 0;
273         }
274         return -EINVAL;
275 }
276
277 static int vidioc_s_ctrl (struct file *file, void *priv,
278                                         struct v4l2_control *ctrl)
279 {
280         struct video_device *dev = video_devdata(file);
281         struct typhoon_device *typhoon = dev->priv;
282
283         switch (ctrl->id) {
284         case V4L2_CID_AUDIO_MUTE:
285                 if (ctrl->value)
286                         typhoon_mute(typhoon);
287                 else
288                         typhoon_unmute(typhoon);
289                 return 0;
290         case V4L2_CID_AUDIO_VOLUME:
291                 typhoon_setvol(typhoon, ctrl->value);
292                 return 0;
293         }
294         return -EINVAL;
295 }
296
297 static int vidioc_g_audio(struct file *file, void *priv,
298                                         struct v4l2_audio *a)
299 {
300         if (a->index > 1)
301                 return -EINVAL;
302
303         strcpy(a->name, "Radio");
304         a->capability = V4L2_AUDCAP_STEREO;
305         return 0;
306 }
307
308 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
309 {
310         *i = 0;
311         return 0;
312 }
313
314 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
315 {
316         if (i != 0)
317                 return -EINVAL;
318         return 0;
319 }
320
321 static int vidioc_s_audio(struct file *file, void *priv,
322                                         struct v4l2_audio *a)
323 {
324         if (a->index != 0)
325                 return -EINVAL;
326         return 0;
327 }
328
329 static struct typhoon_device typhoon_unit =
330 {
331         .iobase         = CONFIG_RADIO_TYPHOON_PORT,
332         .curfreq        = CONFIG_RADIO_TYPHOON_MUTEFREQ,
333         .mutefreq       = CONFIG_RADIO_TYPHOON_MUTEFREQ,
334 };
335
336 static const struct file_operations typhoon_fops = {
337         .owner          = THIS_MODULE,
338         .open           = video_exclusive_open,
339         .release        = video_exclusive_release,
340         .ioctl          = video_ioctl2,
341 #ifdef CONFIG_COMPAT
342         .compat_ioctl   = v4l_compat_ioctl32,
343 #endif
344         .llseek         = no_llseek,
345 };
346
347 static struct video_device typhoon_radio =
348 {
349         .owner          = THIS_MODULE,
350         .name           = "Typhoon Radio",
351         .type           = VID_TYPE_TUNER,
352         .fops           = &typhoon_fops,
353         .vidioc_querycap    = vidioc_querycap,
354         .vidioc_g_tuner     = vidioc_g_tuner,
355         .vidioc_s_tuner     = vidioc_s_tuner,
356         .vidioc_g_audio     = vidioc_g_audio,
357         .vidioc_s_audio     = vidioc_s_audio,
358         .vidioc_g_input     = vidioc_g_input,
359         .vidioc_s_input     = vidioc_s_input,
360         .vidioc_g_frequency = vidioc_g_frequency,
361         .vidioc_s_frequency = vidioc_s_frequency,
362         .vidioc_queryctrl   = vidioc_queryctrl,
363         .vidioc_g_ctrl      = vidioc_g_ctrl,
364         .vidioc_s_ctrl      = vidioc_s_ctrl,
365 };
366
367 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
368
369 static int typhoon_proc_show(struct seq_file *m, void *v)
370 {
371         #ifdef MODULE
372             #define MODULEPROCSTRING "Driver loaded as a module"
373         #else
374             #define MODULEPROCSTRING "Driver compiled into kernel"
375         #endif
376
377         seq_puts(m, BANNER);
378         seq_puts(m, "Load type: " MODULEPROCSTRING "\n\n");
379         seq_printf(m, "frequency = %lu kHz\n",
380                 typhoon_unit.curfreq >> 4);
381         seq_printf(m, "volume = %d\n", typhoon_unit.curvol);
382         seq_printf(m, "mute = %s\n", typhoon_unit.muted ?
383                 "on" : "off");
384         seq_printf(m, "iobase = 0x%x\n", typhoon_unit.iobase);
385         seq_printf(m, "mute frequency = %lu kHz\n",
386                 typhoon_unit.mutefreq >> 4);
387         return 0;
388 }
389
390 static int typhoon_proc_open(struct inode *inode, struct file *file)
391 {
392         return single_open(file, typhoon_proc_show, NULL);
393 }
394
395 static const struct file_operations typhoon_proc_fops = {
396         .owner          = THIS_MODULE,
397         .open           = typhoon_proc_open,
398         .read           = seq_read,
399         .llseek         = seq_lseek,
400         .release        = single_release,
401 };
402 #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
403
404 MODULE_AUTHOR("Dr. Henrik Seidel");
405 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
406 MODULE_LICENSE("GPL");
407
408 static int io = -1;
409 static int radio_nr = -1;
410
411 module_param(io, int, 0);
412 MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
413 module_param(radio_nr, int, 0);
414
415 #ifdef MODULE
416 static unsigned long mutefreq;
417 module_param(mutefreq, ulong, 0);
418 MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
419 #endif
420
421 static int __init typhoon_init(void)
422 {
423 #ifdef MODULE
424         if (io == -1) {
425                 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
426                 return -EINVAL;
427         }
428         typhoon_unit.iobase = io;
429
430         if (mutefreq < 87000 || mutefreq > 108500) {
431                 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
432                 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
433                 return -EINVAL;
434         }
435         typhoon_unit.mutefreq = mutefreq;
436 #endif /* MODULE */
437
438         printk(KERN_INFO BANNER);
439         mutex_init(&typhoon_unit.lock);
440         io = typhoon_unit.iobase;
441         if (!request_region(io, 8, "typhoon")) {
442                 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
443                        typhoon_unit.iobase);
444                 return -EBUSY;
445         }
446
447         typhoon_radio.priv = &typhoon_unit;
448         if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
449         {
450                 release_region(io, 8);
451                 return -EINVAL;
452         }
453         printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
454         printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
455                typhoon_unit.mutefreq);
456         typhoon_unit.mutefreq <<= 4;
457
458         /* mute card - prevents noisy bootups */
459         typhoon_mute(&typhoon_unit);
460
461 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
462         if (!proc_create("driver/radio-typhoon", 0, NULL, &typhoon_proc_fops))
463                 printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
464 #endif
465
466         return 0;
467 }
468
469 static void __exit typhoon_cleanup_module(void)
470 {
471
472 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
473         remove_proc_entry("driver/radio-typhoon", NULL);
474 #endif
475
476         video_unregister_device(&typhoon_radio);
477         release_region(io, 8);
478 }
479
480 module_init(typhoon_init);
481 module_exit(typhoon_cleanup_module);
482