Import 2.1.118
[davej-history.git] / drivers / char / radio-zoltrix.c
blob115606ac33127eaf986b31b97867744a91723c13
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
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* check_region, request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev.h> /* kernel radio structs */
24 #include <linux/config.h> /* CONFIG_RADIO_ZOLTRIX_PORT */
26 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
27 #define CONFIG_RADIO_ZOLTRIX_PORT -1
28 #endif
30 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
31 static int users = 0;
33 struct zol_device {
34 int port;
35 int curvol;
36 unsigned long curfreq;
37 int muted;
41 /* local things */
43 static void sleep_delay(long n)
45 /* Sleep nicely for 'n' uS */
46 int d = n / (1000000 / HZ);
47 if (!d)
48 udelay(n);
49 else {
50 /* Yield CPU time */
51 unsigned long x = jiffies;
52 while ((jiffies - x) <= d)
53 schedule();
57 static void zol_mute(struct zol_device *dev)
59 dev->muted = 1;
60 outb(0, io);
61 outb(0, io);
62 inb(io + 3); /* Zoltrix needs to be read to confirm */
65 static void zol_on(int vol)
67 int l;
68 outb(vol, io);
69 sleep_delay(10000);
70 l = inb(io + 2);
73 static int zol_setvol(struct zol_device *dev, int vol)
75 if (vol == dev->curvol) { /* requested volume = current */
76 if (dev->muted) { /* user is unmuting the card */
77 dev->muted = 0;
78 zol_on(vol);
80 return 0;
82 if (vol == 0) { /* volume = 0 means mute the card */
83 zol_mute(dev);
84 return 0;
86 dev->muted = 0;
87 dev->curvol = vol;
89 zol_on(vol);
91 return 0;
94 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
96 /* tunes the radio to the desired frequency */
97 unsigned long long bitmask, f, m;
98 int i;
100 m = (freq * 25 / 4 - 8800) * 2;
101 f = (unsigned long long) m + 0x4d1c;
103 bitmask = 0xc480402c10080000ull;
104 i = 45;
106 zol_mute(dev);
108 outb(0x40, io);
109 outb(0xc0, io);
111 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( /*stereo */ 0 << 31));
112 while (i--) {
113 if ((bitmask & 0x8000000000000000ull) != 0) {
114 outb(0x80, io);
115 sleep_delay(50);
116 outb(0x00, io);
117 sleep_delay(50);
118 outb(0x80, io);
119 sleep_delay(50);
120 } else {
121 outb(0xc0, io);
122 sleep_delay(50);
123 outb(0x40, io);
124 sleep_delay(50);
125 outb(0xc0, io);
126 sleep_delay(50);
128 bitmask *= 2;
130 /* termination sequence */
131 outb(0x80, io);
132 outb(0xc0, io);
133 outb(0x40, io);
134 zol_on(dev->curvol);
135 return 0;
138 /* Get signal strength */
140 int zol_getsigstr(struct zol_device *dev)
142 int a, b;
144 outb(0x00, io); /* This stuff I found to do nothing */
145 outb(dev->curvol, io);
146 sleep_delay(20000);
148 a = inb(io);
149 sleep_delay(1000);
150 b = inb(io);
152 if ((a == b) && (a == 0xdf)) /* I found this out by playing */
153 /* with a binary scanner on the card io */
154 return (1);
155 else
156 return (0);
158 if (inb(io) & 2) /* bit set = no signal present */
159 return 0;
160 return 1; /* signal present */
163 static int zol_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
165 struct zol_device *rt = dev->priv;
167 switch (cmd) {
168 case VIDIOCGCAP:
170 struct video_capability v;
171 v.type = VID_TYPE_TUNER;
172 v.channels = 1;
173 v.audios = 1;
174 /* No we don't do pictures */
175 v.maxwidth = 0;
176 v.maxheight = 0;
177 v.minwidth = 0;
178 v.minheight = 0;
179 strcpy(v.name, "Zoltrix Radio");
180 if (copy_to_user(arg, &v, sizeof(v)))
181 return -EFAULT;
182 return 0;
184 case VIDIOCGTUNER:
186 struct video_tuner v;
187 if (copy_from_user(&v, arg, sizeof(v)) != 0)
188 return -EFAULT;
189 if (v.tuner) /* Only 1 tuner */
190 return -EINVAL;
191 v.rangelow = (int) (88.0 * 16);
192 v.rangehigh = (int) (108.0 * 16);
193 v.flags = 0;
194 v.mode = VIDEO_MODE_AUTO;
195 v.signal = 0xFFFF * zol_getsigstr(rt);
196 if (copy_to_user(arg, &v, sizeof(v)))
197 return -EFAULT;
198 return 0;
200 case VIDIOCSTUNER:
202 struct video_tuner v;
203 if (copy_from_user(&v, arg, sizeof(v)))
204 return -EFAULT;
205 if (v.tuner != 0)
206 return -EINVAL;
207 /* Only 1 tuner so no setting needed ! */
208 return 0;
210 case VIDIOCGFREQ:
211 if (copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
212 return -EFAULT;
213 return 0;
214 case VIDIOCSFREQ:
215 if (copy_from_user(&rt->curfreq, arg, sizeof(rt->curfreq)))
216 return -EFAULT;
217 zol_setfreq(rt, rt->curfreq);
218 return 0;
219 case VIDIOCGAUDIO:
221 struct video_audio v;
222 memset(&v, 0, sizeof(v));
223 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
224 v.volume = rt->curvol * 4096;
225 v.step = 4096;
226 strcpy(v.name, "Radio");
227 if (copy_to_user(arg, &v, sizeof(v)))
228 return -EFAULT;
229 return 0;
231 case VIDIOCSAUDIO:
233 struct video_audio v;
234 if (copy_from_user(&v, arg, sizeof(v)))
235 return -EFAULT;
236 if (v.audio)
237 return -EINVAL;
239 if (v.flags & VIDEO_AUDIO_MUTE)
240 zol_mute(rt);
241 else
242 zol_setvol(rt, v.volume / 4096);
244 return 0;
246 default:
247 return -ENOIOCTLCMD;
251 static int zol_open(struct video_device *dev, int flags)
253 if (users)
254 return -EBUSY;
255 users++;
256 MOD_INC_USE_COUNT;
257 return 0;
260 static void zol_close(struct video_device *dev)
262 users--;
263 MOD_DEC_USE_COUNT;
266 static struct zol_device zoltrix_unit;
268 static struct video_device zoltrix_radio =
270 "Zoltrix Radio Plus",
271 VID_TYPE_TUNER,
272 VID_HARDWARE_ZOLTRIX,
273 zol_open,
274 zol_close,
275 NULL, /* Can't read (no capture ability) */
276 NULL, /* Can't write */
277 NULL,
278 zol_ioctl,
279 NULL,
280 NULL
283 __initfunc(int zoltrix_init(struct video_init *v))
285 if (check_region(io, 2)) {
286 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
287 return -EBUSY;
289 zoltrix_radio.priv = &zoltrix_unit;
291 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO) == -1)
292 return -EINVAL;
294 request_region(io, 2, "zoltrix");
295 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
297 /* mute card - prevents noisy bootups */
299 /* this ensures that the volume is all the way down */
301 outb(0, io);
302 outb(0, io);
303 sleep_delay(20000);
304 inb(io + 3);
306 zoltrix_unit.curvol = 0;
308 return 0;
311 #ifdef MODULE
313 MODULE_AUTHOR("C.van Schaik");
314 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
315 MODULE_PARM(io, "i");
316 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
318 EXPORT_NO_SYMBOLS;
320 int init_module(void)
322 if (io == -1) {
323 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
324 return -EINVAL;
326 return zoltrix_init(NULL);
329 void cleanup_module(void)
331 video_unregister_device(&zoltrix_radio);
332 release_region(io, 2);
335 #endif