Fix compile error. DEC guys, nobody of you compiling this stuff or
[linux-2.6/linux-mips.git] / drivers / char / radio-zoltrix.c
blobdd688935ab57d205c3bf1fbc9233ebea1c2aaed3
1 /* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
4 * BUGS
5 * Due to the inconsistancy in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
8 * It seems that the card is not linear to 0 volume. It cuts off
9 * at a low volume, and it is not possible (at least I have not found)
10 * to get fine volume control over the low volume range.
12 * Some code derived from code by Romolo Manfredini
13 * romolo@bicnet.it
15 * 1999-05-06 - (C. van Schaik)
16 * - Make signal strength and stereo scans
17 * kinder to cpu while in delay
18 * 1999-01-05 - (C. van Schaik)
19 * - Changed tuning to 1/160Mhz accuracy
20 * - Added stereo support
21 * (card defaults to stereo)
22 * (can explicitly force mono on the card)
23 * (can detect if station is in stereo)
24 * - Added unmute function
25 * - Reworked ioctl functions
28 #include <linux/module.h> /* Modules */
29 #include <linux/init.h> /* Initdata */
30 #include <linux/ioport.h> /* check_region, request_region */
31 #include <linux/delay.h> /* udelay */
32 #include <asm/io.h> /* outb, outb_p */
33 #include <asm/uaccess.h> /* copy to/from user */
34 #include <linux/videodev.h> /* kernel radio structs */
35 #include <linux/config.h> /* CONFIG_RADIO_ZOLTRIX_PORT */
37 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
38 #define CONFIG_RADIO_ZOLTRIX_PORT -1
39 #endif
41 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
42 static int users = 0;
44 struct zol_device {
45 int port;
46 int curvol;
47 unsigned long curfreq;
48 int muted;
49 unsigned int stereo;
50 struct semaphore lock;
54 /* local things */
56 static void sleep_delay(void)
58 /* Sleep nicely for +/- 10 mS */
59 schedule();
62 static int zol_setvol(struct zol_device *dev, int vol)
64 dev->curvol = vol;
65 if (dev->muted)
66 return 0;
68 down(&dev->lock);
69 if (vol == 0) {
70 outb(0, io);
71 outb(0, io);
72 inb(io + 3); /* Zoltrix needs to be read to confirm */
73 up(&dev->lock);
74 return 0;
77 outb(dev->curvol-1, io);
78 sleep_delay();
79 inb(io + 2);
80 up(&dev->lock);
81 return 0;
84 static void zol_mute(struct zol_device *dev)
86 dev->muted = 1;
87 down(&dev->lock);
88 outb(0, io);
89 outb(0, io);
90 inb(io + 3); /* Zoltrix needs to be read to confirm */
91 up(&dev->lock);
94 static void zol_unmute(struct zol_device *dev)
96 dev->muted = 0;
97 zol_setvol(dev, dev->curvol);
100 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
102 /* tunes the radio to the desired frequency */
103 unsigned long long bitmask, f, m;
104 unsigned int stereo = dev->stereo;
105 int i;
107 if (freq == 0)
108 return 1;
109 m = (freq / 160 - 8800) * 2;
110 f = (unsigned long long) m + 0x4d1c;
112 bitmask = 0xc480402c10080000ull;
113 i = 45;
115 down(&dev->lock);
117 outb(0, io);
118 outb(0, io);
119 inb(io + 3); /* Zoltrix needs to be read to confirm */
121 outb(0x40, io);
122 outb(0xc0, io);
124 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
125 while (i--) {
126 if ((bitmask & 0x8000000000000000ull) != 0) {
127 outb(0x80, io);
128 udelay(50);
129 outb(0x00, io);
130 udelay(50);
131 outb(0x80, io);
132 udelay(50);
133 } else {
134 outb(0xc0, io);
135 udelay(50);
136 outb(0x40, io);
137 udelay(50);
138 outb(0xc0, io);
139 udelay(50);
141 bitmask *= 2;
143 /* termination sequence */
144 outb(0x80, io);
145 outb(0xc0, io);
146 outb(0x40, io);
147 udelay(1000);
148 inb(io+2);
150 udelay(1000);
152 if (dev->muted)
154 outb(0, io);
155 outb(0, io);
156 inb(io + 3);
157 udelay(1000);
160 up(&dev->lock);
162 if(!dev->muted)
164 zol_setvol(dev, dev->curvol);
166 return 0;
169 /* Get signal strength */
171 int zol_getsigstr(struct zol_device *dev)
173 int a, b;
175 down(&dev->lock);
176 outb(0x00, io); /* This stuff I found to do nothing */
177 outb(dev->curvol, io);
178 sleep_delay();
179 sleep_delay();
181 a = inb(io);
182 sleep_delay();
183 b = inb(io);
185 up(&dev->lock);
187 if (a != b)
188 return (0);
190 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
191 || (a == 0xef)) /* with a binary scanner on the card io */
192 return (1);
193 return (0);
196 int zol_is_stereo (struct zol_device *dev)
198 int x1, x2;
200 down(&dev->lock);
202 outb(0x00, io);
203 outb(dev->curvol, io);
204 sleep_delay();
205 sleep_delay();
207 x1 = inb(io);
208 sleep_delay();
209 x2 = inb(io);
211 up(&dev->lock);
213 if ((x1 == x2) && (x1 == 0xcf))
214 return 1;
215 return 0;
218 static int zol_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
220 struct zol_device *zol = dev->priv;
222 switch (cmd) {
223 case VIDIOCGCAP:
225 struct video_capability v;
226 v.type = VID_TYPE_TUNER;
227 v.channels = 1 + zol->stereo;
228 v.audios = 1;
229 /* No we don't do pictures */
230 v.maxwidth = 0;
231 v.maxheight = 0;
232 v.minwidth = 0;
233 v.minheight = 0;
234 strcpy(v.name, "Zoltrix Radio");
235 if (copy_to_user(arg, &v, sizeof(v)))
236 return -EFAULT;
237 return 0;
239 case VIDIOCGTUNER:
241 struct video_tuner v;
242 if (copy_from_user(&v, arg, sizeof(v)))
243 return -EFAULT;
244 if (v.tuner)
245 return -EINVAL;
246 strcpy(v.name, "FM");
247 v.rangelow = (int) (88.0 * 16000);
248 v.rangehigh = (int) (108.0 * 16000);
249 v.flags = zol_is_stereo(zol)
250 ? VIDEO_TUNER_STEREO_ON : 0;
251 v.flags |= VIDEO_TUNER_LOW;
252 v.mode = VIDEO_MODE_AUTO;
253 v.signal = 0xFFFF * zol_getsigstr(zol);
254 if (copy_to_user(arg, &v, sizeof(v)))
255 return -EFAULT;
256 return 0;
258 case VIDIOCSTUNER:
260 struct video_tuner v;
261 if (copy_from_user(&v, arg, sizeof(v)))
262 return -EFAULT;
263 if (v.tuner != 0)
264 return -EINVAL;
265 /* Only 1 tuner so no setting needed ! */
266 return 0;
268 case VIDIOCGFREQ:
269 if (copy_to_user(arg, &zol->curfreq, sizeof(zol->curfreq)))
270 return -EFAULT;
271 return 0;
272 case VIDIOCSFREQ:
273 if (copy_from_user(&zol->curfreq, arg, sizeof(zol->curfreq)))
274 return -EFAULT;
275 zol_setfreq(zol, zol->curfreq);
276 return 0;
277 case VIDIOCGAUDIO:
279 struct video_audio v;
280 memset(&v, 0, sizeof(v));
281 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
282 v.mode != zol_is_stereo(zol)
283 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
284 v.volume = zol->curvol * 4096;
285 v.step = 4096;
286 strcpy(v.name, "Zoltrix Radio");
287 if (copy_to_user(arg, &v, sizeof(v)))
288 return -EFAULT;
289 return 0;
291 case VIDIOCSAUDIO:
293 struct video_audio v;
294 if (copy_from_user(&v, arg, sizeof(v)))
295 return -EFAULT;
296 if (v.audio)
297 return -EINVAL;
299 if (v.flags & VIDEO_AUDIO_MUTE)
300 zol_mute(zol);
301 else
303 zol_unmute(zol);
304 zol_setvol(zol, v.volume / 4096);
307 if (v.mode & VIDEO_SOUND_STEREO)
309 zol->stereo = 1;
310 zol_setfreq(zol, zol->curfreq);
312 if (v.mode & VIDEO_SOUND_MONO)
314 zol->stereo = 0;
315 zol_setfreq(zol, zol->curfreq);
318 return 0;
320 default:
321 return -ENOIOCTLCMD;
325 static int zol_open(struct video_device *dev, int flags)
327 if (users)
328 return -EBUSY;
329 users++;
330 MOD_INC_USE_COUNT;
331 return 0;
334 static void zol_close(struct video_device *dev)
336 users--;
337 MOD_DEC_USE_COUNT;
340 static struct zol_device zoltrix_unit;
342 static struct video_device zoltrix_radio =
344 "Zoltrix Radio Plus",
345 VID_TYPE_TUNER,
346 VID_HARDWARE_ZOLTRIX,
347 zol_open,
348 zol_close,
349 NULL, /* Can't read (no capture ability) */
350 NULL, /* Can't write */
351 NULL,
352 zol_ioctl,
353 NULL,
354 NULL
357 static int __init zoltrix_init(void)
359 if (io == -1) {
360 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
361 return -EINVAL;
363 if (check_region(io, 2)) {
364 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
365 return -EBUSY;
367 if ((io != 0x20c) && (io != 0x30c)) {
368 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
369 return -ENXIO;
371 zoltrix_radio.priv = &zoltrix_unit;
373 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO) == -1)
374 return -EINVAL;
376 request_region(io, 2, "zoltrix");
377 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
379 init_MUTEX(&zoltrix_unit.lock);
381 /* mute card - prevents noisy bootups */
383 /* this ensures that the volume is all the way down */
385 outb(0, io);
386 outb(0, io);
387 sleep_delay();
388 sleep_delay();
389 inb(io + 3);
391 zoltrix_unit.curvol = 0;
392 zoltrix_unit.stereo = 1;
394 return 0;
397 MODULE_AUTHOR("C.van Schaik");
398 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
399 MODULE_PARM(io, "i");
400 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
402 EXPORT_NO_SYMBOLS;
404 static void __exit zoltrix_cleanup_module(void)
406 video_unregister_device(&zoltrix_radio);
407 release_region(io, 2);
410 module_init(zoltrix_init);
411 module_exit(zoltrix_cleanup_module);