Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / radio-aimslab.c
blobd716c54bd368bd64102f594bf2f7e3f6c3ed1da3
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 */
38 #include <asm/semaphore.h> /* Lock for the I/O */
40 #ifndef CONFIG_RADIO_RTRACK_PORT
41 #define CONFIG_RADIO_RTRACK_PORT -1
42 #endif
44 static int io = CONFIG_RADIO_RTRACK_PORT;
45 static int users = 0;
46 static struct semaphore lock;
48 struct rt_device
50 int port;
51 int curvol;
52 unsigned long curfreq;
53 int muted;
57 /* local things */
59 static void sleep_delay(long n)
61 /* Sleep nicely for 'n' uS */
62 int d=n/(1000000/HZ);
63 if(!d)
64 udelay(n);
65 else
67 /* Yield CPU time */
68 unsigned long x=jiffies;
69 while((jiffies-x)<=d)
70 schedule();
74 static void rt_decvol(void)
76 outb(0x58, io); /* volume down + sigstr + on */
77 sleep_delay(100000);
78 outb(0xd8, io); /* volume steady + sigstr + on */
81 static void rt_incvol(void)
83 outb(0x98, io); /* volume up + sigstr + on */
84 sleep_delay(100000);
85 outb(0xd8, io); /* volume steady + sigstr + on */
88 static void rt_mute(struct rt_device *dev)
90 dev->muted = 1;
91 down(&lock);
92 outb(0xd0, io); /* volume steady, off */
93 up(&lock);
96 static int rt_setvol(struct rt_device *dev, int vol)
98 int i;
100 down(&lock);
102 if(vol == dev->curvol) { /* requested volume = current */
103 if (dev->muted) { /* user is unmuting the card */
104 dev->muted = 0;
105 outb (0xd8, io); /* enable card */
107 up(&lock);
108 return 0;
111 if(vol == 0) { /* volume = 0 means mute the card */
112 outb(0x48, io); /* volume down but still "on" */
113 sleep_delay(2000000); /* make sure it's totally down */
114 outb(0xd0, io); /* volume steady, off */
115 dev->curvol = 0; /* track the volume state! */
116 up(&lock);
117 return 0;
120 dev->muted = 0;
121 if(vol > dev->curvol)
122 for(i = dev->curvol; i < vol; i++)
123 rt_incvol();
124 else
125 for(i = dev->curvol; i > vol; i--)
126 rt_decvol();
128 dev->curvol = vol;
129 up(&lock);
130 return 0;
133 /* the 128+64 on these outb's is to keep the volume stable while tuning
134 * without them, the volume _will_ creep up with each frequency change
135 * and bit 4 (+16) is to keep the signal strength meter enabled
138 void send_0_byte(int port, struct rt_device *dev)
140 if ((dev->curvol == 0) || (dev->muted)) {
141 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
142 outb_p(128+64+16+2+1, port); /* clock */
144 else {
145 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
146 outb_p(128+64+16+8+2+1, port); /* clock */
148 sleep_delay(1000);
151 void send_1_byte(int port, struct rt_device *dev)
153 if ((dev->curvol == 0) || (dev->muted)) {
154 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
155 outb_p(128+64+16+4+2+1, port); /* clock */
157 else {
158 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
159 outb_p(128+64+16+8+4+2+1, port); /* clock */
162 sleep_delay(1000);
165 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
167 int i;
169 /* adapted from radio-aztech.c */
171 /* now uses VIDEO_TUNER_LOW for fine tuning */
173 freq += 171200; /* Add 10.7 MHz IF */
174 freq /= 800; /* Convert to 50 kHz units */
176 down(&lock); /* Stop other ops interfering */
178 send_0_byte (io, dev); /* 0: LSB of frequency */
180 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
181 if (freq & (1 << i))
182 send_1_byte (io, dev);
183 else
184 send_0_byte (io, dev);
186 send_0_byte (io, dev); /* 14: test bit - always 0 */
187 send_0_byte (io, dev); /* 15: test bit - always 0 */
189 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
190 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
191 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
192 send_0_byte (io, dev); /* 19: time base - always 0 */
194 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
195 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
196 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
197 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
199 if ((dev->curvol == 0) || (dev->muted))
200 outb (0xd0, io); /* volume steady + sigstr */
201 else
202 outb (0xd8, io); /* volume steady + sigstr + on */
204 up(&lock);
206 return 0;
209 static int rt_getsigstr(struct rt_device *dev)
211 if (inb(io) & 2) /* bit set = no signal present */
212 return 0;
213 return 1; /* signal present */
216 static int rt_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
218 struct rt_device *rt=dev->priv;
220 switch(cmd)
222 case VIDIOCGCAP:
224 struct video_capability v;
225 v.type=VID_TYPE_TUNER;
226 v.channels=1;
227 v.audios=1;
228 /* No we don't do pictures */
229 v.maxwidth=0;
230 v.maxheight=0;
231 v.minwidth=0;
232 v.minheight=0;
233 strcpy(v.name, "RadioTrack");
234 if(copy_to_user(arg,&v,sizeof(v)))
235 return -EFAULT;
236 return 0;
238 case VIDIOCGTUNER:
240 struct video_tuner v;
241 if(copy_from_user(&v, arg,sizeof(v))!=0)
242 return -EFAULT;
243 if(v.tuner) /* Only 1 tuner */
244 return -EINVAL;
245 v.rangelow=(87*16000);
246 v.rangehigh=(108*16000);
247 v.flags=VIDEO_TUNER_LOW;
248 v.mode=VIDEO_MODE_AUTO;
249 strcpy(v.name, "FM");
250 v.signal=0xFFFF*rt_getsigstr(rt);
251 if(copy_to_user(arg,&v, sizeof(v)))
252 return -EFAULT;
253 return 0;
255 case VIDIOCSTUNER:
257 struct video_tuner v;
258 if(copy_from_user(&v, arg, sizeof(v)))
259 return -EFAULT;
260 if(v.tuner!=0)
261 return -EINVAL;
262 /* Only 1 tuner so no setting needed ! */
263 return 0;
265 case VIDIOCGFREQ:
266 if(copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
267 return -EFAULT;
268 return 0;
269 case VIDIOCSFREQ:
270 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
271 return -EFAULT;
272 rt_setfreq(rt, rt->curfreq);
273 return 0;
274 case VIDIOCGAUDIO:
276 struct video_audio v;
277 memset(&v,0, sizeof(v));
278 v.flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
279 v.volume=rt->curvol * 6554;
280 v.step=6554;
281 strcpy(v.name, "Radio");
282 if(copy_to_user(arg,&v, sizeof(v)))
283 return -EFAULT;
284 return 0;
286 case VIDIOCSAUDIO:
288 struct video_audio v;
289 if(copy_from_user(&v, arg, sizeof(v)))
290 return -EFAULT;
291 if(v.audio)
292 return -EINVAL;
294 if(v.flags&VIDEO_AUDIO_MUTE)
295 rt_mute(rt);
296 else
297 rt_setvol(rt,v.volume/6554);
299 return 0;
301 default:
302 return -ENOIOCTLCMD;
306 static int rt_open(struct video_device *dev, int flags)
308 if(users)
309 return -EBUSY;
310 users++;
311 MOD_INC_USE_COUNT;
312 return 0;
315 static void rt_close(struct video_device *dev)
317 users--;
318 MOD_DEC_USE_COUNT;
321 static struct rt_device rtrack_unit;
323 static struct video_device rtrack_radio=
325 "RadioTrack radio",
326 VID_TYPE_TUNER,
327 VID_HARDWARE_RTRACK,
328 rt_open,
329 rt_close,
330 NULL, /* Can't read (no capture ability) */
331 NULL, /* Can't write */
332 NULL, /* No poll */
333 rt_ioctl,
334 NULL,
335 NULL
338 static int __init rtrack_init(void)
340 if(io==-1)
342 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
343 return -EINVAL;
346 if (check_region(io, 2))
348 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
349 return -EBUSY;
352 rtrack_radio.priv=&rtrack_unit;
354 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO)==-1)
355 return -EINVAL;
357 request_region(io, 2, "rtrack");
358 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
360 /* Set up the I/O locking */
362 init_MUTEX(&lock);
364 /* mute card - prevents noisy bootups */
366 /* this ensures that the volume is all the way down */
367 outb(0x48, io); /* volume down but still "on" */
368 sleep_delay(2000000); /* make sure it's totally down */
369 outb(0xc0, io); /* steady volume, mute card */
370 rtrack_unit.curvol = 0;
372 return 0;
375 MODULE_AUTHOR("M.Kirkwood");
376 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
377 MODULE_PARM(io, "i");
378 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
380 EXPORT_NO_SYMBOLS;
382 static void __exit cleanup_rtrack_module(void)
384 video_unregister_device(&rtrack_radio);
385 release_region(io,2);
388 module_init(rtrack_init);
389 module_exit(cleanup_rtrack_module);