blob: f8b732bc9a05dbf9d1c208809d6b852f3ae76233 [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 */
38#include <asm/io.h> /* outb, outb_p */
39#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030040#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030041#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030043#include <linux/version.h> /* for KERNEL_VERSION MACRO */
44#define RADIO_VERSION KERNEL_VERSION(0,1,1)
45#define BANNER "Typhoon Radio Card driver v0.1.1\n"
46
47static struct v4l2_queryctrl radio_qctrl[] = {
48 {
49 .id = V4L2_CID_AUDIO_MUTE,
50 .name = "Mute",
51 .minimum = 0,
52 .maximum = 1,
53 .default_value = 1,
54 .type = V4L2_CTRL_TYPE_BOOLEAN,
55 },{
56 .id = V4L2_CID_AUDIO_VOLUME,
57 .name = "Volume",
58 .minimum = 0,
59 .maximum = 65535,
60 .step = 1<<14,
61 .default_value = 0xff,
62 .type = V4L2_CTRL_TYPE_INTEGER,
63 }
64};
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#ifndef CONFIG_RADIO_TYPHOON_PORT
68#define CONFIG_RADIO_TYPHOON_PORT -1
69#endif
70
71#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
72#define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
73#endif
74
75#ifndef CONFIG_PROC_FS
76#undef CONFIG_RADIO_TYPHOON_PROC_FS
77#endif
78
79struct typhoon_device {
80 int users;
81 int iobase;
82 int curvol;
83 int muted;
84 unsigned long curfreq;
85 unsigned long mutefreq;
Ingo Molnar3593cab2006-02-07 06:49:14 -020086 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
89static void typhoon_setvol_generic(struct typhoon_device *dev, int vol);
90static int typhoon_setfreq_generic(struct typhoon_device *dev,
91 unsigned long frequency);
92static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency);
93static void typhoon_mute(struct typhoon_device *dev);
94static void typhoon_unmute(struct typhoon_device *dev);
95static int typhoon_setvol(struct typhoon_device *dev, int vol);
96static int typhoon_ioctl(struct inode *inode, struct file *file,
97 unsigned int cmd, unsigned long arg);
98#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
99static int typhoon_get_info(char *buf, char **start, off_t offset, int len);
100#endif
101
102static void typhoon_setvol_generic(struct typhoon_device *dev, int vol)
103{
Ingo Molnar3593cab2006-02-07 06:49:14 -0200104 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 vol >>= 14; /* Map 16 bit to 2 bit */
106 vol &= 3;
107 outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */
108 outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200109 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static int typhoon_setfreq_generic(struct typhoon_device *dev,
113 unsigned long frequency)
114{
115 unsigned long outval;
116 unsigned long x;
117
118 /*
119 * The frequency transfer curve is not linear. The best fit I could
120 * get is
121 *
122 * outval = -155 + exp((f + 15.55) * 0.057))
123 *
124 * where frequency f is in MHz. Since we don't have exp in the kernel,
125 * I approximate this function by a third order polynomial.
126 *
127 */
128
Ingo Molnar3593cab2006-02-07 06:49:14 -0200129 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 x = frequency / 160;
131 outval = (x * x + 2500) / 5000;
132 outval = (outval * x + 5000) / 10000;
133 outval -= (10 * x * x + 10433) / 20866;
134 outval += 4 * x - 11505;
135
136 outb_p((outval >> 8) & 0x01, dev->iobase + 4);
137 outb_p(outval >> 9, dev->iobase + 6);
138 outb_p(outval & 0xff, dev->iobase + 8);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200139 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return 0;
142}
143
144static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency)
145{
146 typhoon_setfreq_generic(dev, frequency);
147 dev->curfreq = frequency;
148 return 0;
149}
150
151static void typhoon_mute(struct typhoon_device *dev)
152{
153 if (dev->muted == 1)
154 return;
155 typhoon_setvol_generic(dev, 0);
156 typhoon_setfreq_generic(dev, dev->mutefreq);
157 dev->muted = 1;
158}
159
160static void typhoon_unmute(struct typhoon_device *dev)
161{
162 if (dev->muted == 0)
163 return;
164 typhoon_setfreq_generic(dev, dev->curfreq);
165 typhoon_setvol_generic(dev, dev->curvol);
166 dev->muted = 0;
167}
168
169static int typhoon_setvol(struct typhoon_device *dev, int vol)
170{
171 if (dev->muted && vol != 0) { /* user is unmuting the card */
172 dev->curvol = vol;
173 typhoon_unmute(dev);
174 return 0;
175 }
176 if (vol == dev->curvol) /* requested volume == current */
177 return 0;
178
179 if (vol == 0) { /* volume == 0 means mute the card */
180 typhoon_mute(dev);
181 dev->curvol = vol;
182 return 0;
183 }
184 typhoon_setvol_generic(dev, vol);
185 dev->curvol = vol;
186 return 0;
187}
188
189
190static int typhoon_do_ioctl(struct inode *inode, struct file *file,
191 unsigned int cmd, void *arg)
192{
193 struct video_device *dev = video_devdata(file);
194 struct typhoon_device *typhoon = dev->priv;
195
196 switch (cmd) {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300197 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300199 struct v4l2_capability *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 memset(v,0,sizeof(*v));
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300201 strlcpy(v->driver, "radio-typhoon", sizeof (v->driver));
202 strlcpy(v->card, "Typhoon Radio", sizeof (v->card));
203 sprintf(v->bus_info,"ISA");
204 v->version = RADIO_VERSION;
205 v->capabilities = V4L2_CAP_TUNER;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208 }
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300209 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300211 struct v4l2_tuner *v = arg;
212
213 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300215
216 memset(v,0,sizeof(*v));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 strcpy(v->name, "FM");
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300218 v->type = V4L2_TUNER_RADIO;
219
220 v->rangelow=(87.5*16000);
221 v->rangehigh=(108*16000);
222 v->rxsubchans =V4L2_TUNER_SUB_MONO;
223 v->capability=V4L2_TUNER_CAP_LOW;
224 v->audmode = V4L2_TUNER_MODE_MONO;
225 v->signal = 0xFFFF; /* We can't get the signal strength */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228 }
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300229 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300231 struct v4l2_tuner *v = arg;
232
233 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237 }
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300238 case VIDIOC_S_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300240 struct v4l2_frequency *f = arg;
241
242 typhoon->curfreq = f->frequency;
243 typhoon_setfreq(typhoon, typhoon->curfreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245 }
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300246 case VIDIOC_G_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 {
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300248 struct v4l2_frequency *f = arg;
249
250 f->type = V4L2_TUNER_RADIO;
251 f->frequency = typhoon->curfreq;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254 }
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300255 case VIDIOC_QUERYCTRL:
256 {
257 struct v4l2_queryctrl *qc = arg;
258 int i;
259
260 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
261 if (qc->id && qc->id == radio_qctrl[i].id) {
262 memcpy(qc, &(radio_qctrl[i]),
263 sizeof(*qc));
264 return (0);
265 }
266 }
267 return -EINVAL;
268 }
269 case VIDIOC_G_CTRL:
270 {
271 struct v4l2_control *ctrl= arg;
272
273 switch (ctrl->id) {
274 case V4L2_CID_AUDIO_MUTE:
275 ctrl->value=typhoon->muted;
276 return (0);
277 case V4L2_CID_AUDIO_VOLUME:
278 ctrl->value=typhoon->curvol;
279 return (0);
280 }
281 return -EINVAL;
282 }
283 case VIDIOC_S_CTRL:
284 {
285 struct v4l2_control *ctrl= arg;
286
287 switch (ctrl->id) {
288 case V4L2_CID_AUDIO_MUTE:
289 if (ctrl->value) {
290 typhoon_mute(typhoon);
291 } else {
292 typhoon_unmute(typhoon);
293 }
294 return (0);
295 case V4L2_CID_AUDIO_VOLUME:
296 typhoon_setvol(typhoon, ctrl->value);
297 return (0);
298 }
299 return -EINVAL;
300 }
301
302 default:
303 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
304 typhon_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306}
307
308static int typhoon_ioctl(struct inode *inode, struct file *file,
309 unsigned int cmd, unsigned long arg)
310{
311 return video_usercopy(inode, file, cmd, arg, typhoon_do_ioctl);
312}
313
314static struct typhoon_device typhoon_unit =
315{
316 .iobase = CONFIG_RADIO_TYPHOON_PORT,
317 .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
318 .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ,
319};
320
321static struct file_operations typhoon_fops = {
322 .owner = THIS_MODULE,
323 .open = video_exclusive_open,
324 .release = video_exclusive_release,
325 .ioctl = typhoon_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200326 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .llseek = no_llseek,
328};
329
330static struct video_device typhoon_radio =
331{
332 .owner = THIS_MODULE,
333 .name = "Typhoon Radio",
334 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -0300335 .hardware = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .fops = &typhoon_fops,
337};
338
339#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
340
341static int typhoon_get_info(char *buf, char **start, off_t offset, int len)
342{
343 char *out = buf;
344
345 #ifdef MODULE
346 #define MODULEPROCSTRING "Driver loaded as a module"
347 #else
348 #define MODULEPROCSTRING "Driver compiled into kernel"
349 #endif
350
351 /* output must be kept under PAGE_SIZE */
352 out += sprintf(out, BANNER);
353 out += sprintf(out, "Load type: " MODULEPROCSTRING "\n\n");
354 out += sprintf(out, "frequency = %lu kHz\n",
355 typhoon_unit.curfreq >> 4);
356 out += sprintf(out, "volume = %d\n", typhoon_unit.curvol);
357 out += sprintf(out, "mute = %s\n", typhoon_unit.muted ?
358 "on" : "off");
359 out += sprintf(out, "iobase = 0x%x\n", typhoon_unit.iobase);
360 out += sprintf(out, "mute frequency = %lu kHz\n",
361 typhoon_unit.mutefreq >> 4);
362 return out - buf;
363}
364
365#endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
366
367MODULE_AUTHOR("Dr. Henrik Seidel");
368MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
369MODULE_LICENSE("GPL");
370
371static int io = -1;
372static int radio_nr = -1;
373
374module_param(io, int, 0);
375MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)");
376module_param(radio_nr, int, 0);
377
378#ifdef MODULE
379static unsigned long mutefreq = 0;
380module_param(mutefreq, ulong, 0);
381MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
382#endif
383
384static int __init typhoon_init(void)
385{
386#ifdef MODULE
387 if (io == -1) {
388 printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
389 return -EINVAL;
390 }
391 typhoon_unit.iobase = io;
392
393 if (mutefreq < 87000 || mutefreq > 108500) {
394 printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n");
395 printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n");
396 return -EINVAL;
397 }
398 typhoon_unit.mutefreq = mutefreq;
399#endif /* MODULE */
400
401 printk(KERN_INFO BANNER);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200402 mutex_init(&typhoon_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 io = typhoon_unit.iobase;
404 if (!request_region(io, 8, "typhoon")) {
405 printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n",
406 typhoon_unit.iobase);
407 return -EBUSY;
408 }
409
410 typhoon_radio.priv = &typhoon_unit;
411 if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1)
412 {
413 release_region(io, 8);
414 return -EINVAL;
415 }
416 printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase);
417 printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n",
418 typhoon_unit.mutefreq);
419 typhoon_unit.mutefreq <<= 4;
420
421 /* mute card - prevents noisy bootups */
422 typhoon_mute(&typhoon_unit);
423
424#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
425 if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300426 typhoon_get_info))
Trent Piepho657de3c2006-06-20 00:30:57 -0300427 printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#endif
429
430 return 0;
431}
432
433static void __exit typhoon_cleanup_module(void)
434{
435
436#ifdef CONFIG_RADIO_TYPHOON_PROC_FS
437 remove_proc_entry("driver/radio-typhoon", NULL);
438#endif
439
440 video_unregister_device(&typhoon_radio);
441 release_region(io, 8);
442}
443
444module_init(typhoon_init);
445module_exit(typhoon_cleanup_module);
446