Linux 2.3.7pre1
[davej-history.git] / drivers / char / radio-aimslab.c
blob99c1b92bcaf3975d7d03ee97ccdeef9713c9ada9
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 * History:
7 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
8 * Fine tuning/VIDEO_TUNER_LOW
9 * Frequency range expanded to start at 87 MHz
11 * TODO: Allow for more than one of these foolish entities :-)
13 * Notes on the hardware (reverse engineered from other peoples'
14 * reverse engineering of AIMS' code :-)
16 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
18 * The signal strength query is unsurprisingly inaccurate. And it seems
19 * to indicate that (on my card, at least) the frequency setting isn't
20 * too great. (I have to tune up .025MHz from what the freq should be
21 * to get a report that the thing is tuned.)
23 * Volume control is (ugh) analogue:
24 * out(port, start_increasing_volume);
25 * wait(a_wee_while);
26 * out(port, stop_changing_the_volume);
30 #include <linux/module.h> /* Modules */
31 #include <linux/init.h> /* Initdata */
32 #include <linux/ioport.h> /* check_region, request_region */
33 #include <linux/delay.h> /* udelay */
34 #include <asm/io.h> /* outb, outb_p */
35 #include <asm/uaccess.h> /* copy to/from user */
36 #include <linux/videodev.h> /* kernel radio structs */
37 #include <linux/config.h> /* CONFIG_RADIO_RTRACK_PORT */
39 #ifndef CONFIG_RADIO_RTRACK_PORT
40 #define CONFIG_RADIO_RTRACK_PORT -1
41 #endif
43 static int io = CONFIG_RADIO_RTRACK_PORT;
44 static int users = 0;
46 struct rt_device
48 int port;
49 int curvol;
50 unsigned long curfreq;
51 int muted;
55 /* local things */
57 static void sleep_delay(long n)
59 /* Sleep nicely for 'n' uS */
60 int d=n/(1000000/HZ);
61 if(!d)
62 udelay(n);
63 else
65 /* Yield CPU time */
66 unsigned long x=jiffies;
67 while((jiffies-x)<=d)
68 schedule();
72 static void rt_decvol(void)
74 outb(0x58, io); /* volume down + sigstr + on */
75 sleep_delay(100000);
76 outb(0xd8, io); /* volume steady + sigstr + on */
79 static void rt_incvol(void)
81 outb(0x98, io); /* volume up + sigstr + on */
82 sleep_delay(100000);
83 outb(0xd8, io); /* volume steady + sigstr + on */
86 static void rt_mute(struct rt_device *dev)
88 dev->muted = 1;
89 outb(0xd0, io); /* volume steady, off */
92 static int rt_setvol(struct rt_device *dev, int vol)
94 int i;
96 if(vol == dev->curvol) { /* requested volume = current */
97 if (dev->muted) { /* user is unmuting the card */
98 dev->muted = 0;
99 outb (0xd8, io); /* enable card */
102 return 0;
105 if(vol == 0) { /* volume = 0 means mute the card */
106 outb(0x48, io); /* volume down but still "on" */
107 sleep_delay(2000000); /* make sure it's totally down */
108 outb(0xd0, io); /* volume steady, off */
109 dev->curvol = 0; /* track the volume state! */
110 return 0;
113 dev->muted = 0;
114 if(vol > dev->curvol)
115 for(i = dev->curvol; i < vol; i++)
116 rt_incvol();
117 else
118 for(i = dev->curvol; i > vol; i--)
119 rt_decvol();
121 dev->curvol = vol;
123 return 0;
126 /* the 128+64 on these outb's is to keep the volume stable while tuning
127 * without them, the volume _will_ creep up with each frequency change
128 * and bit 4 (+16) is to keep the signal strength meter enabled
131 void send_0_byte(int port, struct rt_device *dev)
133 if ((dev->curvol == 0) || (dev->muted)) {
134 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
135 outb_p(128+64+16+2+1, port); /* clock */
137 else {
138 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
139 outb_p(128+64+16+8+2+1, port); /* clock */
141 sleep_delay(1000);
144 void send_1_byte(int port, struct rt_device *dev)
146 if ((dev->curvol == 0) || (dev->muted)) {
147 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
148 outb_p(128+64+16+4+2+1, port); /* clock */
150 else {
151 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
152 outb_p(128+64+16+8+4+2+1, port); /* clock */
155 sleep_delay(1000);
158 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
160 int i;
162 /* adapted from radio-aztech.c */
164 /* now uses VIDEO_TUNER_LOW for fine tuning */
166 freq += 171200; /* Add 10.7 MHz IF */
167 freq /= 800; /* Convert to 50 kHz units */
169 send_0_byte (io, dev); /* 0: LSB of frequency */
171 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
172 if (freq & (1 << i))
173 send_1_byte (io, dev);
174 else
175 send_0_byte (io, dev);
177 send_0_byte (io, dev); /* 14: test bit - always 0 */
178 send_0_byte (io, dev); /* 15: test bit - always 0 */
180 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
181 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
182 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
183 send_0_byte (io, dev); /* 19: time base - always 0 */
185 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
186 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
187 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
188 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
190 if ((dev->curvol == 0) || (dev->muted))
191 outb (0xd0, io); /* volume steady + sigstr */
192 else
193 outb (0xd8, io); /* volume steady + sigstr + on */
195 return 0;
198 int rt_getsigstr(struct rt_device *dev)
200 if (inb(io) & 2) /* bit set = no signal present */
201 return 0;
202 return 1; /* signal present */
205 static int rt_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
207 struct rt_device *rt=dev->priv;
209 switch(cmd)
211 case VIDIOCGCAP:
213 struct video_capability v;
214 v.type=VID_TYPE_TUNER;
215 v.channels=1;
216 v.audios=1;
217 /* No we don't do pictures */
218 v.maxwidth=0;
219 v.maxheight=0;
220 v.minwidth=0;
221 v.minheight=0;
222 strcpy(v.name, "RadioTrack");
223 if(copy_to_user(arg,&v,sizeof(v)))
224 return -EFAULT;
225 return 0;
227 case VIDIOCGTUNER:
229 struct video_tuner v;
230 if(copy_from_user(&v, arg,sizeof(v))!=0)
231 return -EFAULT;
232 if(v.tuner) /* Only 1 tuner */
233 return -EINVAL;
234 v.rangelow=(87*16000);
235 v.rangehigh=(108*16000);
236 v.flags=VIDEO_TUNER_LOW;
237 v.mode=VIDEO_MODE_AUTO;
238 strcpy(v.name, "FM");
239 v.signal=0xFFFF*rt_getsigstr(rt);
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, &rt->curfreq, sizeof(rt->curfreq)))
256 return -EFAULT;
257 return 0;
258 case VIDIOCSFREQ:
259 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
260 return -EFAULT;
261 rt_setfreq(rt, rt->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.volume=rt->curvol * 6554;
269 v.step=6554;
270 strcpy(v.name, "Radio");
271 if(copy_to_user(arg,&v, sizeof(v)))
272 return -EFAULT;
273 return 0;
275 case VIDIOCSAUDIO:
277 struct video_audio v;
278 if(copy_from_user(&v, arg, sizeof(v)))
279 return -EFAULT;
280 if(v.audio)
281 return -EINVAL;
283 if(v.flags&VIDEO_AUDIO_MUTE)
284 rt_mute(rt);
285 else
286 rt_setvol(rt,v.volume/6554);
288 return 0;
290 default:
291 return -ENOIOCTLCMD;
295 static int rt_open(struct video_device *dev, int flags)
297 if(users)
298 return -EBUSY;
299 users++;
300 MOD_INC_USE_COUNT;
301 return 0;
304 static void rt_close(struct video_device *dev)
306 users--;
307 MOD_DEC_USE_COUNT;
310 static struct rt_device rtrack_unit;
312 static struct video_device rtrack_radio=
314 "RadioTrack radio",
315 VID_TYPE_TUNER,
316 VID_HARDWARE_RTRACK,
317 rt_open,
318 rt_close,
319 NULL, /* Can't read (no capture ability) */
320 NULL, /* Can't write */
321 NULL, /* No poll */
322 rt_ioctl,
323 NULL,
324 NULL
327 __initfunc(int rtrack_init(struct video_init *v))
329 if (check_region(io, 2))
331 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
332 return -EBUSY;
335 rtrack_radio.priv=&rtrack_unit;
337 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO)==-1)
338 return -EINVAL;
340 request_region(io, 2, "rtrack");
341 printk(KERN_INFO "AIMSlab Radiotrack/radioreveal card driver.\n");
343 /* mute card - prevents noisy bootups */
345 /* this ensures that the volume is all the way down */
346 outb(0x48, io); /* volume down but still "on" */
347 sleep_delay(2000000); /* make sure it's totally down */
348 outb(0xc0, io); /* steady volume, mute card */
349 rtrack_unit.curvol = 0;
351 return 0;
354 #ifdef MODULE
356 MODULE_AUTHOR("M.Kirkwood");
357 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
358 MODULE_PARM(io, "i");
359 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
361 EXPORT_NO_SYMBOLS;
363 int init_module(void)
365 if(io==-1)
367 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
368 return -EINVAL;
370 return rtrack_init(NULL);
373 void cleanup_module(void)
375 video_unregister_device(&rtrack_radio);
376 release_region(io,2);
379 #endif