Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
32 | #include <linux/module.h> /* Modules */ | |
33 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 34 | #include <linux/ioport.h> /* request_region */ |
1da177e4 LT |
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 */ | |
5e87efa3 | 39 | #include <media/v4l2-common.h> |
1da177e4 LT |
40 | #include <linux/config.h> /* CONFIG_RADIO_TYPHOON_* */ |
41 | ||
42 | #define BANNER "Typhoon Radio Card driver v0.1\n" | |
43 | ||
44 | #ifndef CONFIG_RADIO_TYPHOON_PORT | |
45 | #define CONFIG_RADIO_TYPHOON_PORT -1 | |
46 | #endif | |
47 | ||
48 | #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ | |
49 | #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0 | |
50 | #endif | |
51 | ||
52 | #ifndef CONFIG_PROC_FS | |
53 | #undef CONFIG_RADIO_TYPHOON_PROC_FS | |
54 | #endif | |
55 | ||
56 | struct typhoon_device { | |
57 | int users; | |
58 | int iobase; | |
59 | int curvol; | |
60 | int muted; | |
61 | unsigned long curfreq; | |
62 | unsigned long mutefreq; | |
3593cab5 | 63 | struct mutex lock; |
1da177e4 LT |
64 | }; |
65 | ||
66 | static void typhoon_setvol_generic(struct typhoon_device *dev, int vol); | |
67 | static int typhoon_setfreq_generic(struct typhoon_device *dev, | |
68 | unsigned long frequency); | |
69 | static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency); | |
70 | static void typhoon_mute(struct typhoon_device *dev); | |
71 | static void typhoon_unmute(struct typhoon_device *dev); | |
72 | static int typhoon_setvol(struct typhoon_device *dev, int vol); | |
73 | static int typhoon_ioctl(struct inode *inode, struct file *file, | |
74 | unsigned int cmd, unsigned long arg); | |
75 | #ifdef CONFIG_RADIO_TYPHOON_PROC_FS | |
76 | static int typhoon_get_info(char *buf, char **start, off_t offset, int len); | |
77 | #endif | |
78 | ||
79 | static void typhoon_setvol_generic(struct typhoon_device *dev, int vol) | |
80 | { | |
3593cab5 | 81 | mutex_lock(&dev->lock); |
1da177e4 LT |
82 | vol >>= 14; /* Map 16 bit to 2 bit */ |
83 | vol &= 3; | |
84 | outb_p(vol / 2, dev->iobase); /* Set the volume, high bit. */ | |
85 | outb_p(vol % 2, dev->iobase + 2); /* Set the volume, low bit. */ | |
3593cab5 | 86 | mutex_unlock(&dev->lock); |
1da177e4 LT |
87 | } |
88 | ||
89 | static int typhoon_setfreq_generic(struct typhoon_device *dev, | |
90 | unsigned long frequency) | |
91 | { | |
92 | unsigned long outval; | |
93 | unsigned long x; | |
94 | ||
95 | /* | |
96 | * The frequency transfer curve is not linear. The best fit I could | |
97 | * get is | |
98 | * | |
99 | * outval = -155 + exp((f + 15.55) * 0.057)) | |
100 | * | |
101 | * where frequency f is in MHz. Since we don't have exp in the kernel, | |
102 | * I approximate this function by a third order polynomial. | |
103 | * | |
104 | */ | |
105 | ||
3593cab5 | 106 | mutex_lock(&dev->lock); |
1da177e4 LT |
107 | x = frequency / 160; |
108 | outval = (x * x + 2500) / 5000; | |
109 | outval = (outval * x + 5000) / 10000; | |
110 | outval -= (10 * x * x + 10433) / 20866; | |
111 | outval += 4 * x - 11505; | |
112 | ||
113 | outb_p((outval >> 8) & 0x01, dev->iobase + 4); | |
114 | outb_p(outval >> 9, dev->iobase + 6); | |
115 | outb_p(outval & 0xff, dev->iobase + 8); | |
3593cab5 | 116 | mutex_unlock(&dev->lock); |
1da177e4 LT |
117 | |
118 | return 0; | |
119 | } | |
120 | ||
121 | static int typhoon_setfreq(struct typhoon_device *dev, unsigned long frequency) | |
122 | { | |
123 | typhoon_setfreq_generic(dev, frequency); | |
124 | dev->curfreq = frequency; | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static void typhoon_mute(struct typhoon_device *dev) | |
129 | { | |
130 | if (dev->muted == 1) | |
131 | return; | |
132 | typhoon_setvol_generic(dev, 0); | |
133 | typhoon_setfreq_generic(dev, dev->mutefreq); | |
134 | dev->muted = 1; | |
135 | } | |
136 | ||
137 | static void typhoon_unmute(struct typhoon_device *dev) | |
138 | { | |
139 | if (dev->muted == 0) | |
140 | return; | |
141 | typhoon_setfreq_generic(dev, dev->curfreq); | |
142 | typhoon_setvol_generic(dev, dev->curvol); | |
143 | dev->muted = 0; | |
144 | } | |
145 | ||
146 | static int typhoon_setvol(struct typhoon_device *dev, int vol) | |
147 | { | |
148 | if (dev->muted && vol != 0) { /* user is unmuting the card */ | |
149 | dev->curvol = vol; | |
150 | typhoon_unmute(dev); | |
151 | return 0; | |
152 | } | |
153 | if (vol == dev->curvol) /* requested volume == current */ | |
154 | return 0; | |
155 | ||
156 | if (vol == 0) { /* volume == 0 means mute the card */ | |
157 | typhoon_mute(dev); | |
158 | dev->curvol = vol; | |
159 | return 0; | |
160 | } | |
161 | typhoon_setvol_generic(dev, vol); | |
162 | dev->curvol = vol; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | ||
167 | static int typhoon_do_ioctl(struct inode *inode, struct file *file, | |
168 | unsigned int cmd, void *arg) | |
169 | { | |
170 | struct video_device *dev = video_devdata(file); | |
171 | struct typhoon_device *typhoon = dev->priv; | |
172 | ||
173 | switch (cmd) { | |
174 | case VIDIOCGCAP: | |
175 | { | |
176 | struct video_capability *v = arg; | |
177 | memset(v,0,sizeof(*v)); | |
178 | v->type = VID_TYPE_TUNER; | |
179 | v->channels = 1; | |
180 | v->audios = 1; | |
181 | strcpy(v->name, "Typhoon Radio"); | |
182 | return 0; | |
183 | } | |
184 | case VIDIOCGTUNER: | |
185 | { | |
186 | struct video_tuner *v = arg; | |
187 | if (v->tuner) /* Only 1 tuner */ | |
188 | return -EINVAL; | |
189 | v->rangelow = 875 * 1600; | |
190 | v->rangehigh = 1080 * 1600; | |
191 | v->flags = VIDEO_TUNER_LOW; | |
192 | v->mode = VIDEO_MODE_AUTO; | |
193 | v->signal = 0xFFFF; /* We can't get the signal strength */ | |
194 | strcpy(v->name, "FM"); | |
195 | return 0; | |
196 | } | |
197 | case VIDIOCSTUNER: | |
198 | { | |
199 | struct video_tuner *v = arg; | |
200 | if (v->tuner != 0) | |
201 | return -EINVAL; | |
202 | /* Only 1 tuner so no setting needed ! */ | |
203 | return 0; | |
204 | } | |
205 | case VIDIOCGFREQ: | |
206 | { | |
207 | unsigned long *freq = arg; | |
208 | *freq = typhoon->curfreq; | |
209 | return 0; | |
210 | } | |
211 | case VIDIOCSFREQ: | |
212 | { | |
213 | unsigned long *freq = arg; | |
214 | typhoon->curfreq = *freq; | |
215 | typhoon_setfreq(typhoon, typhoon->curfreq); | |
216 | return 0; | |
217 | } | |
218 | case VIDIOCGAUDIO: | |
219 | { | |
220 | struct video_audio *v = arg; | |
221 | memset(v, 0, sizeof(*v)); | |
222 | v->flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME; | |
223 | v->mode |= VIDEO_SOUND_MONO; | |
224 | v->volume = typhoon->curvol; | |
225 | v->step = 1 << 14; | |
226 | strcpy(v->name, "Typhoon Radio"); | |
227 | return 0; | |
228 | } | |
229 | case VIDIOCSAUDIO: | |
230 | { | |
231 | struct video_audio *v = arg; | |
232 | if (v->audio) | |
233 | return -EINVAL; | |
234 | if (v->flags & VIDEO_AUDIO_MUTE) | |
235 | typhoon_mute(typhoon); | |
236 | else | |
237 | typhoon_unmute(typhoon); | |
238 | if (v->flags & VIDEO_AUDIO_VOLUME) | |
239 | typhoon_setvol(typhoon, v->volume); | |
240 | return 0; | |
241 | } | |
242 | default: | |
243 | return -ENOIOCTLCMD; | |
244 | } | |
245 | } | |
246 | ||
247 | static int typhoon_ioctl(struct inode *inode, struct file *file, | |
248 | unsigned int cmd, unsigned long arg) | |
249 | { | |
250 | return video_usercopy(inode, file, cmd, arg, typhoon_do_ioctl); | |
251 | } | |
252 | ||
253 | static struct typhoon_device typhoon_unit = | |
254 | { | |
255 | .iobase = CONFIG_RADIO_TYPHOON_PORT, | |
256 | .curfreq = CONFIG_RADIO_TYPHOON_MUTEFREQ, | |
257 | .mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ, | |
258 | }; | |
259 | ||
260 | static struct file_operations typhoon_fops = { | |
261 | .owner = THIS_MODULE, | |
262 | .open = video_exclusive_open, | |
263 | .release = video_exclusive_release, | |
264 | .ioctl = typhoon_ioctl, | |
0d0fbf81 | 265 | .compat_ioctl = v4l_compat_ioctl32, |
1da177e4 LT |
266 | .llseek = no_llseek, |
267 | }; | |
268 | ||
269 | static struct video_device typhoon_radio = | |
270 | { | |
271 | .owner = THIS_MODULE, | |
272 | .name = "Typhoon Radio", | |
273 | .type = VID_TYPE_TUNER, | |
274 | .hardware = VID_HARDWARE_TYPHOON, | |
275 | .fops = &typhoon_fops, | |
276 | }; | |
277 | ||
278 | #ifdef CONFIG_RADIO_TYPHOON_PROC_FS | |
279 | ||
280 | static int typhoon_get_info(char *buf, char **start, off_t offset, int len) | |
281 | { | |
282 | char *out = buf; | |
283 | ||
284 | #ifdef MODULE | |
285 | #define MODULEPROCSTRING "Driver loaded as a module" | |
286 | #else | |
287 | #define MODULEPROCSTRING "Driver compiled into kernel" | |
288 | #endif | |
289 | ||
290 | /* output must be kept under PAGE_SIZE */ | |
291 | out += sprintf(out, BANNER); | |
292 | out += sprintf(out, "Load type: " MODULEPROCSTRING "\n\n"); | |
293 | out += sprintf(out, "frequency = %lu kHz\n", | |
294 | typhoon_unit.curfreq >> 4); | |
295 | out += sprintf(out, "volume = %d\n", typhoon_unit.curvol); | |
296 | out += sprintf(out, "mute = %s\n", typhoon_unit.muted ? | |
297 | "on" : "off"); | |
298 | out += sprintf(out, "iobase = 0x%x\n", typhoon_unit.iobase); | |
299 | out += sprintf(out, "mute frequency = %lu kHz\n", | |
300 | typhoon_unit.mutefreq >> 4); | |
301 | return out - buf; | |
302 | } | |
303 | ||
304 | #endif /* CONFIG_RADIO_TYPHOON_PROC_FS */ | |
305 | ||
306 | MODULE_AUTHOR("Dr. Henrik Seidel"); | |
307 | MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio)."); | |
308 | MODULE_LICENSE("GPL"); | |
309 | ||
310 | static int io = -1; | |
311 | static int radio_nr = -1; | |
312 | ||
313 | module_param(io, int, 0); | |
314 | MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)"); | |
315 | module_param(radio_nr, int, 0); | |
316 | ||
317 | #ifdef MODULE | |
318 | static unsigned long mutefreq = 0; | |
319 | module_param(mutefreq, ulong, 0); | |
320 | MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)"); | |
321 | #endif | |
322 | ||
323 | static int __init typhoon_init(void) | |
324 | { | |
325 | #ifdef MODULE | |
326 | if (io == -1) { | |
327 | printk(KERN_ERR "radio-typhoon: You must set an I/O address with io=0x316 or io=0x336\n"); | |
328 | return -EINVAL; | |
329 | } | |
330 | typhoon_unit.iobase = io; | |
331 | ||
332 | if (mutefreq < 87000 || mutefreq > 108500) { | |
333 | printk(KERN_ERR "radio-typhoon: You must set a frequency (in kHz) used when muting the card,\n"); | |
334 | printk(KERN_ERR "radio-typhoon: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n"); | |
335 | return -EINVAL; | |
336 | } | |
337 | typhoon_unit.mutefreq = mutefreq; | |
338 | #endif /* MODULE */ | |
339 | ||
340 | printk(KERN_INFO BANNER); | |
3593cab5 | 341 | mutex_init(&typhoon_unit.lock); |
1da177e4 LT |
342 | io = typhoon_unit.iobase; |
343 | if (!request_region(io, 8, "typhoon")) { | |
344 | printk(KERN_ERR "radio-typhoon: port 0x%x already in use\n", | |
345 | typhoon_unit.iobase); | |
346 | return -EBUSY; | |
347 | } | |
348 | ||
349 | typhoon_radio.priv = &typhoon_unit; | |
350 | if (video_register_device(&typhoon_radio, VFL_TYPE_RADIO, radio_nr) == -1) | |
351 | { | |
352 | release_region(io, 8); | |
353 | return -EINVAL; | |
354 | } | |
355 | printk(KERN_INFO "radio-typhoon: port 0x%x.\n", typhoon_unit.iobase); | |
356 | printk(KERN_INFO "radio-typhoon: mute frequency is %lu kHz.\n", | |
357 | typhoon_unit.mutefreq); | |
358 | typhoon_unit.mutefreq <<= 4; | |
359 | ||
360 | /* mute card - prevents noisy bootups */ | |
361 | typhoon_mute(&typhoon_unit); | |
362 | ||
363 | #ifdef CONFIG_RADIO_TYPHOON_PROC_FS | |
364 | if (!create_proc_info_entry("driver/radio-typhoon", 0, NULL, | |
4286c6f6 | 365 | typhoon_get_info)) |
1da177e4 LT |
366 | printk(KERN_ERR "radio-typhoon: registering /proc/driver/radio-typhoon failed\n"); |
367 | #endif | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | static void __exit typhoon_cleanup_module(void) | |
373 | { | |
374 | ||
375 | #ifdef CONFIG_RADIO_TYPHOON_PROC_FS | |
376 | remove_proc_entry("driver/radio-typhoon", NULL); | |
377 | #endif | |
378 | ||
379 | video_unregister_device(&typhoon_radio); | |
380 | release_region(io, 8); | |
381 | } | |
382 | ||
383 | module_init(typhoon_init); | |
384 | module_exit(typhoon_cleanup_module); | |
385 |