Import 2.1.118
[davej-history.git] / drivers / char / radio-aimslab.c
blob59d95042ee86c4efe4684761717f55832118dc5c
1 /* radiotrack (radioreveal) driver for Linux radio support
2 * (c) 1997 M. Kirkwood
3 * Coverted to new API by Alan Cox <Alan.Cox@linux.org>
4 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
6 * TODO: Allow for more than one of these foolish entities :-)
8 * Notes on the hardware (reverse engineered from other peoples'
9 * reverse engineering of AIMS' code :-)
11 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
13 * The signal strength query is unsurprisingly inaccurate. And it seems
14 * to indicate that (on my card, at least) the frequency setting isn't
15 * too great. (I have to tune up .025MHz from what the freq should be
16 * to get a report that the thing is tuned.)
18 * Volume control is (ugh) analogue:
19 * out(port, start_increasing_volume);
20 * wait(a_wee_while);
21 * out(port, stop_changing_the_volume);
25 #include <linux/module.h> /* Modules */
26 #include <linux/init.h> /* Initdata */
27 #include <linux/ioport.h> /* check_region, request_region */
28 #include <linux/delay.h> /* udelay */
29 #include <asm/io.h> /* outb, outb_p */
30 #include <asm/uaccess.h> /* copy to/from user */
31 #include <linux/videodev.h> /* kernel radio structs */
32 #include <linux/config.h> /* CONFIG_RADIO_RTRACK_PORT */
34 #ifndef CONFIG_RADIO_RTRACK_PORT
35 #define CONFIG_RADIO_RTRACK_PORT -1
36 #endif
38 static int io = CONFIG_RADIO_RTRACK_PORT;
39 static int users = 0;
41 struct rt_device
43 int port;
44 int curvol;
45 unsigned long curfreq;
46 int muted;
50 /* local things */
52 static void sleep_delay(long n)
54 /* Sleep nicely for 'n' uS */
55 int d=n/(1000000/HZ);
56 if(!d)
57 udelay(n);
58 else
60 /* Yield CPU time */
61 unsigned long x=jiffies;
62 while((jiffies-x)<=d)
63 schedule();
67 static void rt_decvol(void)
69 outb(0x58, io); /* volume down + sigstr + on */
70 sleep_delay(100000);
71 outb(0xd8, io); /* volume steady + sigstr + on */
74 static void rt_incvol(void)
76 outb(0x98, io); /* volume up + sigstr + on */
77 sleep_delay(100000);
78 outb(0xd8, io); /* volume steady + sigstr + on */
81 static void rt_mute(struct rt_device *dev)
83 dev->muted = 1;
84 outb(0xd0, io); /* volume steady, off */
87 static int rt_setvol(struct rt_device *dev, int vol)
89 int i;
91 if(vol == dev->curvol) { /* requested volume = current */
92 if (dev->muted) { /* user is unmuting the card */
93 dev->muted = 0;
94 outb (0xd8, io); /* enable card */
97 return 0;
100 if(vol == 0) { /* volume = 0 means mute the card */
101 outb(0x48, io); /* volume down but still "on" */
102 sleep_delay(2000000); /* make sure it's totally down */
103 outb(0xd0, io); /* volume steady, off */
104 return 0;
107 dev->muted = 0;
108 if(vol > dev->curvol)
109 for(i = dev->curvol; i < vol; i++)
110 rt_incvol();
111 else
112 for(i = dev->curvol; i > vol; i--)
113 rt_decvol();
115 dev->curvol = vol;
117 return 0;
120 /* the 128+64 on these outb's is to keep the volume stable while tuning
121 * without them, the volume _will_ creep up with each frequency change
122 * and bit 4 (+16) is to keep the signal strength meter enabled
125 void send_0_byte(int port, struct rt_device *dev)
127 if ((dev->curvol == 0) || (dev->muted)) {
128 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
129 outb_p(128+64+16+2+1, port); /* clock */
131 else {
132 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
133 outb_p(128+64+16+8+2+1, port); /* clock */
135 sleep_delay(1000);
138 void send_1_byte(int port, struct rt_device *dev)
140 if ((dev->curvol == 0) || (dev->muted)) {
141 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
142 outb_p(128+64+16+4+2+1, port); /* clock */
144 else {
145 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
146 outb_p(128+64+16+8+4+2+1, port); /* clock */
149 sleep_delay(1000);
152 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
154 int i;
156 /* adapted from radio-aztech.c */
158 /* We want to compute x * 100 / 16 without overflow
159 * So we compute x*6 + (x/100)*25 to give x*6.25
162 freq = freq * 6 + freq/4; /* massage the data a little */
163 freq += 1070; /* IF = 10.7 MHz */
164 freq /= 5; /* ref = 25 kHz */
166 send_0_byte (io, dev); /* 0: LSB of frequency */
168 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
169 if (freq & (1 << i))
170 send_1_byte (io, dev);
171 else
172 send_0_byte (io, dev);
174 send_0_byte (io, dev); /* 14: test bit - always 0 */
175 send_0_byte (io, dev); /* 15: test bit - always 0 */
177 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
178 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
179 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
180 send_0_byte (io, dev); /* 19: time base - always 0 */
182 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
183 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
184 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
185 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
187 if ((dev->curvol == 0) || (dev->muted))
188 outb (0xd0, io); /* volume steady + sigstr */
189 else
190 outb (0xd8, io); /* volume steady + sigstr + on */
192 return 0;
195 int rt_getsigstr(struct rt_device *dev)
197 if (inb(io) & 2) /* bit set = no signal present */
198 return 0;
199 return 1; /* signal present */
202 static int rt_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
204 struct rt_device *rt=dev->priv;
206 switch(cmd)
208 case VIDIOCGCAP:
210 struct video_capability v;
211 v.type=VID_TYPE_TUNER;
212 v.channels=1;
213 v.audios=1;
214 /* No we don't do pictures */
215 v.maxwidth=0;
216 v.maxheight=0;
217 v.minwidth=0;
218 v.minheight=0;
219 strcpy(v.name, "RadioTrack");
220 if(copy_to_user(arg,&v,sizeof(v)))
221 return -EFAULT;
222 return 0;
224 case VIDIOCGTUNER:
226 struct video_tuner v;
227 if(copy_from_user(&v, arg,sizeof(v))!=0)
228 return -EFAULT;
229 if(v.tuner) /* Only 1 tuner */
230 return -EINVAL;
231 v.rangelow=(88*16);
232 v.rangehigh=(108*16);
233 v.flags=0;
234 v.mode=VIDEO_MODE_AUTO;
235 v.signal=0xFFFF*rt_getsigstr(rt);
236 if(copy_to_user(arg,&v, sizeof(v)))
237 return -EFAULT;
238 return 0;
240 case VIDIOCSTUNER:
242 struct video_tuner v;
243 if(copy_from_user(&v, arg, sizeof(v)))
244 return -EFAULT;
245 if(v.tuner!=0)
246 return -EINVAL;
247 /* Only 1 tuner so no setting needed ! */
248 return 0;
250 case VIDIOCGFREQ:
251 if(copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
252 return -EFAULT;
253 return 0;
254 case VIDIOCSFREQ:
255 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
256 return -EFAULT;
257 rt_setfreq(rt, rt->curfreq);
258 return 0;
259 case VIDIOCGAUDIO:
261 struct video_audio v;
262 memset(&v,0, sizeof(v));
263 v.flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
264 v.volume=rt->curvol * 6554;
265 v.step=6554;
266 strcpy(v.name, "Radio");
267 if(copy_to_user(arg,&v, sizeof(v)))
268 return -EFAULT;
269 return 0;
271 case VIDIOCSAUDIO:
273 struct video_audio v;
274 if(copy_from_user(&v, arg, sizeof(v)))
275 return -EFAULT;
276 if(v.audio)
277 return -EINVAL;
279 if(v.flags&VIDEO_AUDIO_MUTE)
280 rt_mute(rt);
281 else
282 rt_setvol(rt,v.volume/6554);
284 return 0;
286 default:
287 return -ENOIOCTLCMD;
291 static int rt_open(struct video_device *dev, int flags)
293 if(users)
294 return -EBUSY;
295 users++;
296 MOD_INC_USE_COUNT;
297 return 0;
300 static void rt_close(struct video_device *dev)
302 users--;
303 MOD_DEC_USE_COUNT;
306 static struct rt_device rtrack_unit;
308 static struct video_device rtrack_radio=
310 "RadioTrack radio",
311 VID_TYPE_TUNER,
312 VID_HARDWARE_RTRACK,
313 rt_open,
314 rt_close,
315 NULL, /* Can't read (no capture ability) */
316 NULL, /* Can't write */
317 NULL, /* No poll */
318 rt_ioctl,
319 NULL,
320 NULL
323 __initfunc(int rtrack_init(struct video_init *v))
325 if (check_region(io, 2))
327 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
328 return -EBUSY;
331 rtrack_radio.priv=&rtrack_unit;
333 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO)==-1)
334 return -EINVAL;
336 request_region(io, 2, "rtrack");
337 printk(KERN_INFO "AIMSlab Radiotrack/radioreveal card driver.\n");
339 /* mute card - prevents noisy bootups */
341 /* this ensures that the volume is all the way down */
342 outb(0x48, io); /* volume down but still "on" */
343 sleep_delay(2000000); /* make sure it's totally down */
344 outb(0xc0, io); /* steady volume, mute card */
345 rtrack_unit.curvol = 0;
347 return 0;
350 #ifdef MODULE
352 MODULE_AUTHOR("M.Kirkwood");
353 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
354 MODULE_PARM(io, "i");
355 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
357 EXPORT_NO_SYMBOLS;
359 int init_module(void)
361 if(io==-1)
363 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
364 return -EINVAL;
366 return rtrack_init(NULL);
369 void cleanup_module(void)
371 video_unregister_device(&rtrack_radio);
372 release_region(io,2);
375 #endif