Import 2.2.0pre6
[davej-history.git] / drivers / char / radio-zoltrix.c
blob80a72d4b4c4d403661c9b21949e375db294e29ad
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 * There seems to be a problem with the volume setting that I must still
9 * figure out.
10 * It seems that the card has is not linear to 0 volume. It cuts off
11 * at a low frequency, and it is not possible (at least I have not found)
12 * to get fine volume control over the low volume range.
14 * Some code derived from code by Frans Brinkman
16 * 1999-01-05 - (C. van Schaik)
17 * - Changed tuning to 1/160Mhz accuracy
18 * - Added stereo support
19 * (card defaults to stereo)
20 * (can explicitly force mono on the card)
21 * (can detect if station is in stereo)
22 * - Added unmute function
23 * - Reworked ioctl functions
26 #include <linux/module.h> /* Modules */
27 #include <linux/init.h> /* Initdata */
28 #include <linux/ioport.h> /* check_region, request_region */
29 #include <linux/delay.h> /* udelay */
30 #include <asm/io.h> /* outb, outb_p */
31 #include <asm/uaccess.h> /* copy to/from user */
32 #include <linux/videodev.h> /* kernel radio structs */
33 #include <linux/config.h> /* CONFIG_RADIO_ZOLTRIX_PORT */
35 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
36 #define CONFIG_RADIO_ZOLTRIX_PORT -1
37 #endif
39 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
40 static int users = 0;
42 struct zol_device {
43 int port;
44 int curvol;
45 unsigned long curfreq;
46 int muted;
47 unsigned int stereo;
51 /* local things */
53 static void sleep_delay(long n)
55 /* Sleep nicely for 'n' uS */
56 int d = n / (1000000 / HZ);
57 if (!d)
58 udelay(n);
59 else {
60 /* Yield CPU time */
61 unsigned long x = jiffies;
62 while ((jiffies - x) <= d)
63 schedule();
67 static int zol_setvol(struct zol_device *dev, int vol)
69 dev->curvol = vol;
70 if (dev->muted)
71 return 0;
73 if (vol == 0) {
74 outb(0, io);
75 outb(0, io);
76 inb(io + 3); /* Zoltrix needs to be read to confirm */
77 return 0;
80 outb(dev->curvol-1, io);
81 sleep_delay(10000);
82 inb(io + 2);
84 return 0;
87 static void zol_mute(struct zol_device *dev)
89 dev->muted = 1;
90 outb(0, io);
91 outb(0, io);
92 inb(io + 3); /* Zoltrix needs to be read to confirm */
95 static void zol_unmute(struct zol_device *dev)
97 dev->muted = 0;
98 zol_setvol(dev, dev->curvol);
101 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
103 /* tunes the radio to the desired frequency */
104 unsigned long long bitmask, f, m;
105 unsigned int stereo = dev->stereo;
106 int i;
108 if (freq == 0)
109 return 1;
110 m = (freq / 160 - 8800) * 2;
111 f = (unsigned long long) m + 0x4d1c;
113 bitmask = 0xc480402c10080000ull;
114 i = 45;
116 outb(0, io);
117 outb(0, io);
118 inb(io + 3); /* Zoltrix needs to be read to confirm */
120 outb(0x40, io);
121 outb(0xc0, io);
123 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
124 while (i--) {
125 if ((bitmask & 0x8000000000000000ull) != 0) {
126 outb(0x80, io);
127 sleep_delay(50);
128 outb(0x00, io);
129 sleep_delay(50);
130 outb(0x80, io);
131 sleep_delay(50);
132 } else {
133 outb(0xc0, io);
134 sleep_delay(50);
135 outb(0x40, io);
136 sleep_delay(50);
137 outb(0xc0, io);
138 sleep_delay(50);
140 bitmask *= 2;
142 /* termination sequence */
143 outb(0x80, io);
144 outb(0xc0, io);
145 outb(0x40, io);
146 sleep_delay(1000);
147 inb(io+2);
149 sleep_delay(1000);
150 if (dev->muted)
152 outb(0, io);
153 outb(0, io);
154 inb(io + 3);
155 sleep_delay(1000);
156 } else
157 zol_setvol(dev, dev->curvol);
158 return 0;
161 /* Get signal strength */
163 int zol_getsigstr(struct zol_device *dev)
165 int a, b;
167 outb(0x00, io); /* This stuff I found to do nothing */
168 outb(dev->curvol, io);
169 sleep_delay(20000);
171 a = inb(io);
172 sleep_delay(1000);
173 b = inb(io);
175 if (a != b)
176 return (0);
178 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
179 || (a == 0xef)) /* with a binary scanner on the card io */
180 return (1);
181 return (0);
184 int zol_is_stereo (struct zol_device *dev)
186 int x1, x2;
188 outb(0x00, io);
189 outb(dev->curvol, io);
190 sleep_delay(20000);
192 x1 = inb(io);
193 sleep_delay(1000);
194 x2 = inb(io);
196 if ((x1 == x2) && (x1 == 0xcf))
197 return 1;
198 return 0;
201 static int zol_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
203 struct zol_device *zol = dev->priv;
205 switch (cmd) {
206 case VIDIOCGCAP:
208 struct video_capability v;
209 v.type = VID_TYPE_TUNER;
210 v.channels = 1 + zol->stereo;
211 v.audios = 1;
212 /* No we don't do pictures */
213 v.maxwidth = 0;
214 v.maxheight = 0;
215 v.minwidth = 0;
216 v.minheight = 0;
217 strcpy(v.name, "Zoltrix Radio");
218 if (copy_to_user(arg, &v, sizeof(v)))
219 return -EFAULT;
220 return 0;
222 case VIDIOCGTUNER:
224 struct video_tuner v;
226 if (copy_from_user(&v, arg, sizeof(v)))
227 return -EFAULT;
228 if (v.tuner)
229 return -EINVAL;
231 v.tuner = 0;
232 strcpy(v.name, "Zoltrix Radio");
233 v.rangelow = (int) (88.0 * 16000);
234 v.rangehigh = (int) (108.0 * 16000);
235 v.flags = zol_is_stereo(zol)
236 ? VIDEO_TUNER_STEREO_ON : 0;
237 v.flags |= VIDEO_TUNER_LOW;
238 v.mode = VIDEO_MODE_AUTO;
239 v.signal = 0xFFFF * zol_getsigstr(zol);
240 if (copy_to_user(arg, &v, sizeof(v)))
241 return -EFAULT;
242 return 0;
244 case VIDIOCSTUNER:
246 struct video_tuner v;
247 if (copy_from_user(&v, arg, sizeof(v)))
248 return -EFAULT;
249 if (v.tuner != 0)
250 return -EINVAL;
251 /* Only 1 tuner so no setting needed ! */
252 return 0;
254 case VIDIOCGFREQ:
255 if (copy_to_user(arg, &zol->curfreq, sizeof(zol->curfreq)))
256 return -EFAULT;
257 return 0;
258 case VIDIOCSFREQ:
259 if (copy_from_user(&zol->curfreq, arg, sizeof(zol->curfreq)))
260 return -EFAULT;
261 zol_setfreq(zol, zol->curfreq);
262 return 0;
263 case VIDIOCGAUDIO:
265 struct video_audio v;
266 memset(&v, 0, sizeof(v));
267 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
268 v.mode != zol_is_stereo(zol)
269 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
270 v.volume = zol->curvol * 4096;
271 v.step = 4096;
272 strcpy(v.name, "Zoltrix Radio");
273 if (copy_to_user(arg, &v, sizeof(v)))
274 return -EFAULT;
275 return 0;
277 case VIDIOCSAUDIO:
279 struct video_audio v;
280 if (copy_from_user(&v, arg, sizeof(v)))
281 return -EFAULT;
282 if (v.audio)
283 return -EINVAL;
285 if (v.flags & VIDEO_AUDIO_MUTE)
286 zol_mute(zol);
287 else
288 zol_unmute(zol);
290 if (v.flags & VIDEO_AUDIO_VOLUME)
291 zol_setvol(zol, v.volume / 4096);
293 if (v.mode & VIDEO_SOUND_STEREO)
295 zol->stereo = 1;
296 zol_setfreq(zol, zol->curfreq);
298 if (v.mode & VIDEO_SOUND_MONO)
300 zol->stereo = 0;
301 zol_setfreq(zol, zol->curfreq);
304 return 0;
306 default:
307 return -ENOIOCTLCMD;
311 static int zol_open(struct video_device *dev, int flags)
313 if (users)
314 return -EBUSY;
315 users++;
316 MOD_INC_USE_COUNT;
317 return 0;
320 static void zol_close(struct video_device *dev)
322 users--;
323 MOD_DEC_USE_COUNT;
326 static struct zol_device zoltrix_unit;
328 static struct video_device zoltrix_radio =
330 "Zoltrix Radio Plus",
331 VID_TYPE_TUNER,
332 VID_HARDWARE_ZOLTRIX,
333 zol_open,
334 zol_close,
335 NULL, /* Can't read (no capture ability) */
336 NULL, /* Can't write */
337 NULL,
338 zol_ioctl,
339 NULL,
340 NULL
343 __initfunc(int zoltrix_init(struct video_init *v))
345 if (check_region(io, 2)) {
346 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
347 return -EBUSY;
349 if ((io != 0x20c) && (io != 0x30c)) {
350 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
351 return -ENXIO;
353 zoltrix_radio.priv = &zoltrix_unit;
355 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO) == -1)
356 return -EINVAL;
358 request_region(io, 2, "zoltrix");
359 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
361 /* mute card - prevents noisy bootups */
363 /* this ensures that the volume is all the way down */
365 outb(0, io);
366 outb(0, io);
367 sleep_delay(20000);
368 inb(io + 3);
370 zoltrix_unit.curvol = 0;
371 zoltrix_unit.stereo = 1;
373 return 0;
376 #ifdef MODULE
378 MODULE_AUTHOR("C.van Schaik");
379 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
380 MODULE_PARM(io, "i");
381 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
383 EXPORT_NO_SYMBOLS;
385 int init_module(void)
387 if (io == -1) {
388 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
389 return -EINVAL;
391 return zoltrix_init(NULL);
394 void cleanup_module(void)
396 video_unregister_device(&zoltrix_radio);
397 release_region(io, 2);
400 #endif