Linux 2.3.7pre1
[davej-history.git] / drivers / char / radio-zoltrix.c
blobf54784811a2b42dc865311bf5b904fe20c9682d3
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;
53 /* local things */
55 static void sleep_delay(void)
57 /* Sleep nicely for +/- 10 mS */
58 schedule();
61 static int zol_setvol(struct zol_device *dev, int vol)
63 dev->curvol = vol;
64 if (dev->muted)
65 return 0;
67 if (vol == 0) {
68 outb(0, io);
69 outb(0, io);
70 inb(io + 3); /* Zoltrix needs to be read to confirm */
71 return 0;
74 outb(dev->curvol-1, io);
75 sleep_delay();
76 inb(io + 2);
78 return 0;
81 static void zol_mute(struct zol_device *dev)
83 dev->muted = 1;
84 outb(0, io);
85 outb(0, io);
86 inb(io + 3); /* Zoltrix needs to be read to confirm */
89 static void zol_unmute(struct zol_device *dev)
91 dev->muted = 0;
92 zol_setvol(dev, dev->curvol);
95 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
97 /* tunes the radio to the desired frequency */
98 unsigned long long bitmask, f, m;
99 unsigned int stereo = dev->stereo;
100 int i;
102 if (freq == 0)
103 return 1;
104 m = (freq / 160 - 8800) * 2;
105 f = (unsigned long long) m + 0x4d1c;
107 bitmask = 0xc480402c10080000ull;
108 i = 45;
110 outb(0, io);
111 outb(0, io);
112 inb(io + 3); /* Zoltrix needs to be read to confirm */
114 outb(0x40, io);
115 outb(0xc0, io);
117 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
118 while (i--) {
119 if ((bitmask & 0x8000000000000000ull) != 0) {
120 outb(0x80, io);
121 udelay(50);
122 outb(0x00, io);
123 udelay(50);
124 outb(0x80, io);
125 udelay(50);
126 } else {
127 outb(0xc0, io);
128 udelay(50);
129 outb(0x40, io);
130 udelay(50);
131 outb(0xc0, io);
132 udelay(50);
134 bitmask *= 2;
136 /* termination sequence */
137 outb(0x80, io);
138 outb(0xc0, io);
139 outb(0x40, io);
140 udelay(1000);
141 inb(io+2);
143 udelay(1000);
144 if (dev->muted)
146 outb(0, io);
147 outb(0, io);
148 inb(io + 3);
149 udelay(1000);
150 } else
151 zol_setvol(dev, dev->curvol);
152 return 0;
155 /* Get signal strength */
157 int zol_getsigstr(struct zol_device *dev)
159 int a, b;
161 outb(0x00, io); /* This stuff I found to do nothing */
162 outb(dev->curvol, io);
163 sleep_delay();
164 sleep_delay();
166 a = inb(io);
167 sleep_delay();
168 b = inb(io);
170 if (a != b)
171 return (0);
173 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
174 || (a == 0xef)) /* with a binary scanner on the card io */
175 return (1);
176 return (0);
179 int zol_is_stereo (struct zol_device *dev)
181 int x1, x2;
183 outb(0x00, io);
184 outb(dev->curvol, io);
185 sleep_delay();
186 sleep_delay();
188 x1 = inb(io);
189 sleep_delay();
190 x2 = inb(io);
192 if ((x1 == x2) && (x1 == 0xcf))
193 return 1;
194 return 0;
197 static int zol_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
199 struct zol_device *zol = dev->priv;
201 switch (cmd) {
202 case VIDIOCGCAP:
204 struct video_capability v;
205 v.type = VID_TYPE_TUNER;
206 v.channels = 1 + zol->stereo;
207 v.audios = 1;
208 /* No we don't do pictures */
209 v.maxwidth = 0;
210 v.maxheight = 0;
211 v.minwidth = 0;
212 v.minheight = 0;
213 strcpy(v.name, "Zoltrix Radio");
214 if (copy_to_user(arg, &v, sizeof(v)))
215 return -EFAULT;
216 return 0;
218 case VIDIOCGTUNER:
220 struct video_tuner v;
221 if (copy_from_user(&v, arg, sizeof(v)))
222 return -EFAULT;
223 if (v.tuner)
224 return -EINVAL;
225 strcpy(v.name, "FM");
226 v.rangelow = (int) (88.0 * 16000);
227 v.rangehigh = (int) (108.0 * 16000);
228 v.flags = zol_is_stereo(zol)
229 ? VIDEO_TUNER_STEREO_ON : 0;
230 v.flags |= VIDEO_TUNER_LOW;
231 v.mode = VIDEO_MODE_AUTO;
232 v.signal = 0xFFFF * zol_getsigstr(zol);
233 if (copy_to_user(arg, &v, sizeof(v)))
234 return -EFAULT;
235 return 0;
237 case VIDIOCSTUNER:
239 struct video_tuner v;
240 if (copy_from_user(&v, arg, sizeof(v)))
241 return -EFAULT;
242 if (v.tuner != 0)
243 return -EINVAL;
244 /* Only 1 tuner so no setting needed ! */
245 return 0;
247 case VIDIOCGFREQ:
248 if (copy_to_user(arg, &zol->curfreq, sizeof(zol->curfreq)))
249 return -EFAULT;
250 return 0;
251 case VIDIOCSFREQ:
252 if (copy_from_user(&zol->curfreq, arg, sizeof(zol->curfreq)))
253 return -EFAULT;
254 zol_setfreq(zol, zol->curfreq);
255 return 0;
256 case VIDIOCGAUDIO:
258 struct video_audio v;
259 memset(&v, 0, sizeof(v));
260 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
261 v.mode != zol_is_stereo(zol)
262 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
263 v.volume = zol->curvol * 4096;
264 v.step = 4096;
265 strcpy(v.name, "Zoltrix Radio");
266 if (copy_to_user(arg, &v, sizeof(v)))
267 return -EFAULT;
268 return 0;
270 case VIDIOCSAUDIO:
272 struct video_audio v;
273 if (copy_from_user(&v, arg, sizeof(v)))
274 return -EFAULT;
275 if (v.audio)
276 return -EINVAL;
278 if (v.flags & VIDEO_AUDIO_MUTE)
279 zol_mute(zol);
280 else
282 zol_unmute(zol);
283 zol_setvol(zol, v.volume / 4096);
286 if (v.mode & VIDEO_SOUND_STEREO)
288 zol->stereo = 1;
289 zol_setfreq(zol, zol->curfreq);
291 if (v.mode & VIDEO_SOUND_MONO)
293 zol->stereo = 0;
294 zol_setfreq(zol, zol->curfreq);
297 return 0;
299 default:
300 return -ENOIOCTLCMD;
304 static int zol_open(struct video_device *dev, int flags)
306 if (users)
307 return -EBUSY;
308 users++;
309 MOD_INC_USE_COUNT;
310 return 0;
313 static void zol_close(struct video_device *dev)
315 users--;
316 MOD_DEC_USE_COUNT;
319 static struct zol_device zoltrix_unit;
321 static struct video_device zoltrix_radio =
323 "Zoltrix Radio Plus",
324 VID_TYPE_TUNER,
325 VID_HARDWARE_ZOLTRIX,
326 zol_open,
327 zol_close,
328 NULL, /* Can't read (no capture ability) */
329 NULL, /* Can't write */
330 NULL,
331 zol_ioctl,
332 NULL,
333 NULL
336 __initfunc(int zoltrix_init(struct video_init *v))
338 if (check_region(io, 2)) {
339 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
340 return -EBUSY;
342 if ((io != 0x20c) && (io != 0x30c)) {
343 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
344 return -ENXIO;
346 zoltrix_radio.priv = &zoltrix_unit;
348 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO) == -1)
349 return -EINVAL;
351 request_region(io, 2, "zoltrix");
352 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
354 /* mute card - prevents noisy bootups */
356 /* this ensures that the volume is all the way down */
358 outb(0, io);
359 outb(0, io);
360 sleep_delay();
361 sleep_delay();
362 inb(io + 3);
364 zoltrix_unit.curvol = 0;
365 zoltrix_unit.stereo = 1;
367 return 0;
370 #ifdef MODULE
372 MODULE_AUTHOR("C.van Schaik");
373 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
374 MODULE_PARM(io, "i");
375 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
377 EXPORT_NO_SYMBOLS;
379 int init_module(void)
381 if (io == -1) {
382 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
383 return -EINVAL;
385 return zoltrix_init(NULL);
388 void cleanup_module(void)
390 video_unregister_device(&zoltrix_radio);
391 release_region(io, 2);
394 #endif