1 /* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
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
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
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/proc_fs.h> /* radio card status report */
38 #include <asm/io.h> /* outb, outb_p */
39 #include <asm/uaccess.h> /* copy to/from user */
40 #include <linux/videodev2.h> /* kernel radio structs */
41 #include <media/v4l2-common.h>
43 #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"
47 static struct v4l2_queryctrl radio_qctrl
[] = {
49 .id
= V4L2_CID_AUDIO_MUTE
,
54 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
56 .id
= V4L2_CID_AUDIO_VOLUME
,
61 .default_value
= 0xff,
62 .type
= V4L2_CTRL_TYPE_INTEGER
,
67 #ifndef CONFIG_RADIO_TYPHOON_PORT
68 #define CONFIG_RADIO_TYPHOON_PORT -1
71 #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
72 #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0
75 #ifndef CONFIG_PROC_FS
76 #undef CONFIG_RADIO_TYPHOON_PROC_FS
79 struct typhoon_device
{
84 unsigned long curfreq
;
85 unsigned long mutefreq
;
89 static void typhoon_setvol_generic(struct typhoon_device
*dev
, int vol
);
90 static int typhoon_setfreq_generic(struct typhoon_device
*dev
,
91 unsigned long frequency
);
92 static int typhoon_setfreq(struct typhoon_device
*dev
, unsigned long frequency
);
93 static void typhoon_mute(struct typhoon_device
*dev
);
94 static void typhoon_unmute(struct typhoon_device
*dev
);
95 static int typhoon_setvol(struct typhoon_device
*dev
, int vol
);
96 static int typhoon_ioctl(struct inode
*inode
, struct file
*file
,
97 unsigned int cmd
, unsigned long arg
);
98 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
99 static int typhoon_get_info(char *buf
, char **start
, off_t offset
, int len
);
102 static void typhoon_setvol_generic(struct typhoon_device
*dev
, int vol
)
104 mutex_lock(&dev
->lock
);
105 vol
>>= 14; /* Map 16 bit to 2 bit */
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. */
109 mutex_unlock(&dev
->lock
);
112 static int typhoon_setfreq_generic(struct typhoon_device
*dev
,
113 unsigned long frequency
)
115 unsigned long outval
;
119 * The frequency transfer curve is not linear. The best fit I could
122 * outval = -155 + exp((f + 15.55) * 0.057))
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.
129 mutex_lock(&dev
->lock
);
131 outval
= (x
* x
+ 2500) / 5000;
132 outval
= (outval
* x
+ 5000) / 10000;
133 outval
-= (10 * x
* x
+ 10433) / 20866;
134 outval
+= 4 * x
- 11505;
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);
139 mutex_unlock(&dev
->lock
);
144 static int typhoon_setfreq(struct typhoon_device
*dev
, unsigned long frequency
)
146 typhoon_setfreq_generic(dev
, frequency
);
147 dev
->curfreq
= frequency
;
151 static void typhoon_mute(struct typhoon_device
*dev
)
155 typhoon_setvol_generic(dev
, 0);
156 typhoon_setfreq_generic(dev
, dev
->mutefreq
);
160 static void typhoon_unmute(struct typhoon_device
*dev
)
164 typhoon_setfreq_generic(dev
, dev
->curfreq
);
165 typhoon_setvol_generic(dev
, dev
->curvol
);
169 static int typhoon_setvol(struct typhoon_device
*dev
, int vol
)
171 if (dev
->muted
&& vol
!= 0) { /* user is unmuting the card */
176 if (vol
== dev
->curvol
) /* requested volume == current */
179 if (vol
== 0) { /* volume == 0 means mute the card */
184 typhoon_setvol_generic(dev
, vol
);
190 static int typhoon_do_ioctl(struct inode
*inode
, struct file
*file
,
191 unsigned int cmd
, void *arg
)
193 struct video_device
*dev
= video_devdata(file
);
194 struct typhoon_device
*typhoon
= dev
->priv
;
197 case VIDIOC_QUERYCAP
:
199 struct v4l2_capability
*v
= arg
;
200 memset(v
,0,sizeof(*v
));
201 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
;
211 struct v4l2_tuner
*v
= arg
;
216 memset(v
,0,sizeof(*v
));
217 strcpy(v
->name
, "FM");
218 v
->type
= V4L2_TUNER_RADIO
;
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 */
231 struct v4l2_tuner
*v
= arg
;
238 case VIDIOC_S_FREQUENCY
:
240 struct v4l2_frequency
*f
= arg
;
242 typhoon
->curfreq
= f
->frequency
;
243 typhoon_setfreq(typhoon
, typhoon
->curfreq
);
246 case VIDIOC_G_FREQUENCY
:
248 struct v4l2_frequency
*f
= arg
;
250 f
->type
= V4L2_TUNER_RADIO
;
251 f
->frequency
= typhoon
->curfreq
;
255 case VIDIOC_QUERYCTRL
:
257 struct v4l2_queryctrl
*qc
= arg
;
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
]),
271 struct v4l2_control
*ctrl
= arg
;
274 case V4L2_CID_AUDIO_MUTE
:
275 ctrl
->value
=typhoon
->muted
;
277 case V4L2_CID_AUDIO_VOLUME
:
278 ctrl
->value
=typhoon
->curvol
;
285 struct v4l2_control
*ctrl
= arg
;
288 case V4L2_CID_AUDIO_MUTE
:
290 typhoon_mute(typhoon
);
292 typhoon_unmute(typhoon
);
295 case V4L2_CID_AUDIO_VOLUME
:
296 typhoon_setvol(typhoon
, ctrl
->value
);
303 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
308 static int typhoon_ioctl(struct inode
*inode
, struct file
*file
,
309 unsigned int cmd
, unsigned long arg
)
311 return video_usercopy(inode
, file
, cmd
, arg
, typhoon_do_ioctl
);
314 static struct typhoon_device typhoon_unit
=
316 .iobase
= CONFIG_RADIO_TYPHOON_PORT
,
317 .curfreq
= CONFIG_RADIO_TYPHOON_MUTEFREQ
,
318 .mutefreq
= CONFIG_RADIO_TYPHOON_MUTEFREQ
,
321 static const struct file_operations typhoon_fops
= {
322 .owner
= THIS_MODULE
,
323 .open
= video_exclusive_open
,
324 .release
= video_exclusive_release
,
325 .ioctl
= typhoon_ioctl
,
326 .compat_ioctl
= v4l_compat_ioctl32
,
330 static struct video_device typhoon_radio
=
332 .owner
= THIS_MODULE
,
333 .name
= "Typhoon Radio",
334 .type
= VID_TYPE_TUNER
,
336 .fops
= &typhoon_fops
,
339 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
341 static int typhoon_get_info(char *buf
, char **start
, off_t offset
, int len
)
346 #define MODULEPROCSTRING "Driver loaded as a module"
348 #define MODULEPROCSTRING "Driver compiled into kernel"
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
?
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);
365 #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */
367 MODULE_AUTHOR("Dr. Henrik Seidel");
368 MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
369 MODULE_LICENSE("GPL");
372 static int radio_nr
= -1;
374 module_param(io
, int, 0);
375 MODULE_PARM_DESC(io
, "I/O address of the Typhoon card (0x316 or 0x336)");
376 module_param(radio_nr
, int, 0);
379 static unsigned long mutefreq
= 0;
380 module_param(mutefreq
, ulong
, 0);
381 MODULE_PARM_DESC(mutefreq
, "Frequency used when muting the card (in kHz)");
384 static int __init
typhoon_init(void)
388 printk(KERN_ERR
"radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n");
391 typhoon_unit
.iobase
= io
;
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");
398 typhoon_unit
.mutefreq
= mutefreq
;
401 printk(KERN_INFO BANNER
);
402 mutex_init(&typhoon_unit
.lock
);
403 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
);
410 typhoon_radio
.priv
= &typhoon_unit
;
411 if (video_register_device(&typhoon_radio
, VFL_TYPE_RADIO
, radio_nr
) == -1)
413 release_region(io
, 8);
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;
421 /* mute card - prevents noisy bootups */
422 typhoon_mute(&typhoon_unit
);
424 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
425 if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL
,
427 printk(KERN_ERR
"radio-typhoon: registering /proc/driver/radio-typhoon failed\n");
433 static void __exit
typhoon_cleanup_module(void)
436 #ifdef CONFIG_RADIO_TYPHOON_PROC_FS
437 remove_proc_entry("driver/radio-typhoon", NULL
);
440 video_unregister_device(&typhoon_radio
);
441 release_region(io
, 8);
444 module_init(typhoon_init
);
445 module_exit(typhoon_cleanup_module
);