* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / char / radio-typhoon.c
blob698f113e8041ac032a01c87df93b127b726b0557
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.
32 #include <linux/module.h> /* Modules */
33 #include <linux/init.h> /* Initdata */
34 #include <linux/ioport.h> /* check_region, request_region */
35 #include <linux/proc_fs.h> /* radio card status report */
36 #include <asm/io.h> /* outb, outb_p */
37 #include <asm/uaccess.h> /* copy to/from user */
38 #include <linux/videodev.h> /* kernel radio structs */
39 #include <linux/config.h> /* CONFIG_RADIO_TYPHOON_* */
41 #define BANNER "Typhoon Radio Card driver v0.1\n"
43 #ifndef CONFIG_RADIO_TYPHOON_PORT
44 #define CONFIG_RADIO_TYPHOON_PORT -1
45 #endif
47 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
48 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
49 #endif
51 #ifndef CONFIG_PROC_FS
52 #undef CONFIG_RADIO_TYPHOON_PROC_FS
53 #endif
55 struct typhoon_device {
56 int users;
57 int iobase;
58 int curvol;
59 int muted;
60 unsigned long curfreq;
61 unsigned long mutefreq;
64 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
65 static int typhoon_setfreq_generic(struct typhoon_device *dev,
66 unsigned long frequency);
67 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
68 static void typhoon_mute(struct typhoon_device *dev);
69 static void typhoon_unmute(struct typhoon_device *dev);
70 static int typhoon_setvol(struct typhoon_device *dev, int vol);
71 static int typhoon_ioctl(struct video_device *dev, unsigned int cmd, void *arg);
72 static int typhoon_open(struct video_device *dev, int flags);
73 static void typhoon_close(struct video_device *dev);
74 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
75 static int typhoon_read_proc(char *buf, char **start, off_t offset, int len,
76 int unused);
77 #endif
78 #ifdef MODULE
79 int init_module(void);
80 void cleanup_module(void);
81 int typhoon_init(struct video_init *v);
82 #else
83 int typhoon_init(struct video_init *v) __init;
84 #endif
86 static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
88 vol >>= 14; /* Map 16 bit to 2 bit */
89 vol &= 3;
90 outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
91 outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
94 static int typhoon_setfreq_generic(struct typhoon_device *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 x = frequency / 160;
112 outval = (x * x + 2500) / 5000;
113 outval = (outval * x + 5000) / 10000;
114 outval -= (10 * x * x + 10433) / 20866;
115 outval += 4 * x - 11505;
117 outb_p((outval >> 8) & 0x01, dev->iobase + 4);
118 outb_p(outval >> 9, dev->iobase + 6);
119 outb_p(outval & 0xff, dev->iobase + 8);
121 return 0;
124 static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
126 typhoon_setfreq_generic(dev, frequency);
127 dev->curfreq = frequency;
128 return 0;
131 static void typhoon_mute(struct typhoon_device *dev)
133 if (dev->muted == 1)
134 return;
135 typhoon_setvol_generic(dev, 0);
136 typhoon_setfreq_generic(dev, dev->mutefreq);
137 dev->muted = 1;
140 static void typhoon_unmute(struct typhoon_device *dev)
142 if (dev->muted == 0)
143 return;
144 typhoon_setfreq_generic(dev, dev->curfreq);
145 typhoon_setvol_generic(dev, dev->curvol);
146 dev->muted = 0;
149 static int typhoon_setvol(struct typhoon_device *dev, int vol)
151 if (dev->muted && vol != 0) { /* user is unmuting the card */
152 dev->curvol = vol;
153 typhoon_unmute(dev);
154 return 0;
156 if (vol == dev->curvol) /* requested volume == current */
157 return 0;
159 if (vol == 0) { /* volume == 0 means mute the card */
160 typhoon_mute(dev);
161 dev->curvol = vol;
162 return 0;
164 typhoon_setvol_generic(dev, vol);
165 dev->curvol = vol;
166 return 0;
170 static int typhoon_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
172 struct typhoon_device *typhoon = dev->priv;
174 switch (cmd) {
175 case VIDIOCGCAP:
177 struct video_capability v;
178 v.type = VID_TYPE_TUNER;
179 v.channels = 1;
180 v.audios = 1;
181 /* No we don't do pictures */
182 v.maxwidth = 0;
183 v.maxheight = 0;
184 v.minwidth = 0;
185 v.minheight = 0;
186 strcpy(v.name, "Typhoon Radio");
187 if (copy_to_user(arg, &v, sizeof(v)))
188 return -EFAULT;
189 return 0;
191 case VIDIOCGTUNER:
193 struct video_tuner v;
194 if (copy_from_user(&v, arg, sizeof(v)) != 0)
195 return -EFAULT;
196 if (v.tuner) /* Only 1 tuner */
197 return -EINVAL;
198 v.rangelow = 875 * 1600;
199 v.rangehigh = 1080 * 1600;
200 v.flags = VIDEO_TUNER_LOW;
201 v.mode = VIDEO_MODE_AUTO;
202 v.signal = 0xFFFF; /* We can't get the signal strength */
203 strcpy(v.name, "FM");
204 if (copy_to_user(arg, &v, sizeof(v)))
205 return -EFAULT;
206 return 0;
208 case VIDIOCSTUNER:
210 struct video_tuner v;
211 if (copy_from_user(&v, arg, sizeof(v)))
212 return -EFAULT;
213 if (v.tuner != 0)
214 return -EINVAL;
215 /* Only 1 tuner so no setting needed ! */
216 return 0;
218 case VIDIOCGFREQ:
219 if (copy_to_user(arg, &typhoon->curfreq,
220 sizeof(typhoon->curfreq)))
221 return -EFAULT;
222 return 0;
223 case VIDIOCSFREQ:
224 if (copy_from_user(&typhoon->curfreq, arg,
225 sizeof(typhoon->curfreq)))
226 return -EFAULT;
227 typhoon_setfreq(typhoon, typhoon->curfreq);
228 return 0;
229 case VIDIOCGAUDIO:
231 struct video_audio v;
232 memset(&v, 0, sizeof(v));
233 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
234 v.mode |= VIDEO_SOUND_MONO;
235 v.volume = typhoon->curvol;
236 v.step = 1 << 14;
237 strcpy(v.name, "Typhoon Radio");
238 if (copy_to_user(arg, &v, sizeof(v)))
239 return -EFAULT;
240 return 0;
242 case VIDIOCSAUDIO:
244 struct video_audio v;
245 if (copy_from_user(&v, arg, sizeof(v)))
246 return -EFAULT;
247 if (v.audio)
248 return -EINVAL;
250 if (v.flags & VIDEO_AUDIO_MUTE)
251 typhoon_mute(typhoon);
252 else
253 typhoon_unmute(typhoon);
255 if (v.flags & VIDEO_AUDIO_VOLUME)
256 typhoon_setvol(typhoon, v.volume);
258 return 0;
260 default:
261 return -ENOIOCTLCMD;
265 static int typhoon_open(struct video_device *dev, int flags)
267 struct typhoon_device *typhoon = dev->priv;
268 if (typhoon->users)
269 return -EBUSY;
270 typhoon->users++;
271 MOD_INC_USE_COUNT;
272 return 0;
275 static void typhoon_close(struct video_device *dev)
277 struct typhoon_device *typhoon = dev->priv;
278 typhoon->users--;
279 MOD_DEC_USE_COUNT;
282 static struct typhoon_device typhoon_unit =
284 0, /* users */
285 CONFIG_RADIO_TYPHOON_PORT, /* iobase */
286 0, /* curvol */
287 0, /* muted */
288 CONFIG_RADIO_TYPHOON_MUTEFREQ, /* curfreq */
289 CONFIG_RADIO_TYPHOON_MUTEFREQ /* mutefreq */
292 static struct video_device typhoon_radio =
294 "Typhoon Radio",
295 VID_TYPE_TUNER,
296 VID_HARDWARE_TYPHOON,
297 typhoon_open,
298 typhoon_close,
299 NULL, /* Can't read (no capture ability) */
300 NULL, /* Can't write */
301 NULL, /* Can't poll */
302 typhoon_ioctl,
303 NULL,
304 NULL
307 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
309 static int typhoon_read_proc(char *buf, char **start, off_t offset, int len,
310 int unused)
312 #ifdef MODULE
313 #define MODULEPROCSTRING "Driver loaded as a module"
314 #else
315 #define MODULEPROCSTRING "Driver compiled into kernel"
316 #endif
318 #define LIMIT (PAGE_SIZE - 80)
320 len = 0;
321 len += sprintf(buf + len, BANNER);
322 if (len > LIMIT) return len;
323 len += sprintf(buf + len, "Load type: " MODULEPROCSTRING "\n\n");
324 if (len > LIMIT) return len;
325 len += sprintf(buf + len, "frequency = %lu kHz\n",
326 typhoon_unit.curfreq >> 4);
327 if (len > LIMIT) return len;
328 len += sprintf(buf + len, "volume = %d\n", typhoon_unit.curvol);
329 if (len > LIMIT) return len;
330 len += sprintf(buf + len, "mute = %s\n", typhoon_unit.muted ?
331 "on" : "off");
332 if (len > LIMIT) return len;
333 len += sprintf(buf + len, "iobase = 0x%x\n", typhoon_unit.iobase);
334 if (len > LIMIT) return len;
335 len += sprintf(buf + len, "mute frequency = %lu kHz\n",
336 typhoon_unit.mutefreq >> 4);
337 return len;
340 static struct proc_dir_entry typhoon_proc_entry = {
341 0, /* low_ino: inode is dynamic */
342 13, "radio-typhoon", /* length of name and name */
343 S_IFREG | S_IRUGO, /* mode */
344 1, 0, 0, /* nlinks, owner, group */
345 0, /* size -- not used */
346 NULL, /* operations -- use default */
347 &typhoon_read_proc, /* function used to read data */
348 /* nothing more */
351 #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
353 int typhoon_init(struct video_init *v)
355 printk(KERN_INFO BANNER);
356 if (check_region(typhoon_unit.iobase, 8)) {
357 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
358 typhoon_unit.iobase);
359 return -EBUSY;
362 typhoon_radio.priv = &typhoon_unit;
363 if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO) == -1)
364 return -EINVAL;
366 request_region(typhoon_unit.iobase, 8, "typhoon");
367 printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
368 printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
369 typhoon_unit.mutefreq);
370 typhoon_unit.mutefreq <<= 4;
372 /* mute card - prevents noisy bootups */
373 typhoon_mute(&typhoon_unit);
375 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
377 if (proc_register(&proc_root, &typhoon_proc_entry))
378 printk(KERN_ERR "radio-typhoon: registering /proc/radio-typhoon failed\n");
380 #endif
382 return 0;
385 #ifdef MODULE
387 MODULE_AUTHOR("Dr. Henrik Seidel");
388 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
389 MODULE_PARM(io, "i");
390 MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
391 MODULE_PARM(mutefreq, "i");
392 MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
394 EXPORT_NO_SYMBOLS;
396 static int io = -1;
397 static unsigned long mutefreq = 0;
399 int init_module(void)
401 if (io == -1) {
402 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
403 return -EINVAL;
405 typhoon_unit.iobase = io;
407 if (mutefreq < 87000 || mutefreq > 108500) {
408 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
409 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
410 return -EINVAL;
412 typhoon_unit.mutefreq = mutefreq;
414 return typhoon_init(NULL);
417 void cleanup_module(void)
420 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
422 if (proc_unregister(&proc_root, typhoon_proc_entry.low_ino))
423 printk(KERN_ERR "radio-typhoon: unregistering /proc/radio-typhoon failed\n");
425 #endif
427 video_unregister_device(&typhoon_radio);
428 release_region(io, 8);
431 #endif