ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / radio / radio-aimslab.c
1 /* radiotrack (radioreveal) driver for Linux radio support
2  * (c) 1997 M. Kirkwood
3  * Converted to new API by Alan Cox <Alan.Cox@linux.org>
4  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
5  *
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
10  *
11  * TODO: Allow for more than one of these foolish entities :-)
12  *
13  * Notes on the hardware (reverse engineered from other peoples'
14  * reverse engineering of AIMS' code :-)
15  *
16  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
17  *
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.)
22  *
23  *  Volume control is (ugh) analogue:
24  *   out(port, start_increasing_volume);
25  *   wait(a_wee_while);
26  *   out(port, stop_changing_the_volume);
27  *  
28  */
29
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             */
39
40 #ifndef CONFIG_RADIO_RTRACK_PORT
41 #define CONFIG_RADIO_RTRACK_PORT -1
42 #endif
43
44 static int io = CONFIG_RADIO_RTRACK_PORT; 
45 static int radio_nr = -1;
46 static struct semaphore lock;
47
48 struct rt_device
49 {
50         int port;
51         int curvol;
52         unsigned long curfreq;
53         int muted;
54 };
55
56
57 /* local things */
58
59 static void sleep_delay(long n)
60 {
61         /* Sleep nicely for 'n' uS */
62         int d=n/(1000000/HZ);
63         if(!d)
64                 udelay(n);
65         else
66         {
67                 /* Yield CPU time */
68                 unsigned long x=jiffies;
69                 while((jiffies-x)<=d)
70                         schedule();
71         }
72 }
73
74 static void rt_decvol(void)
75 {
76         outb(0x58, io);         /* volume down + sigstr + on    */
77         sleep_delay(100000);
78         outb(0xd8, io);         /* volume steady + sigstr + on  */
79 }
80
81 static void rt_incvol(void)
82 {
83         outb(0x98, io);         /* volume up + sigstr + on      */
84         sleep_delay(100000);
85         outb(0xd8, io);         /* volume steady + sigstr + on  */
86 }
87
88 static void rt_mute(struct rt_device *dev)
89 {
90         dev->muted = 1;
91         down(&lock);
92         outb(0xd0, io);                 /* volume steady, off           */
93         up(&lock);
94 }
95
96 static int rt_setvol(struct rt_device *dev, int vol)
97 {
98         int i;
99
100         down(&lock);
101         
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 */
106                 }       
107                 up(&lock);
108                 return 0;
109         }
110
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;
118         }
119
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();
127
128         dev->curvol = vol;
129         up(&lock);
130         return 0;
131 }
132
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
136  */
137
138 void send_0_byte(int port, struct rt_device *dev)
139 {
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 */
143         }
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 */
147         }
148         sleep_delay(1000); 
149 }
150
151 void send_1_byte(int port, struct rt_device *dev)
152 {
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 */
156         } 
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 */
160         }
161
162         sleep_delay(1000); 
163 }
164
165 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
166 {
167         int i;
168
169         /* adapted from radio-aztech.c */
170
171         /* now uses VIDEO_TUNER_LOW for fine tuning */
172
173         freq += 171200;                 /* Add 10.7 MHz IF              */
174         freq /= 800;                    /* Convert to 50 kHz units      */
175         
176         down(&lock);                    /* Stop other ops interfering */
177          
178         send_0_byte (io, dev);          /*  0: LSB of frequency         */
179
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);
185
186         send_0_byte (io, dev);          /* 14: test bit - always 0    */
187         send_0_byte (io, dev);          /* 15: test bit - always 0    */
188
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   */
193
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) */
198
199         if ((dev->curvol == 0) || (dev->muted))
200                 outb (0xd0, io);        /* volume steady + sigstr */
201         else
202                 outb (0xd8, io);        /* volume steady + sigstr + on */
203                 
204         up(&lock);
205
206         return 0;
207 }
208
209 static int rt_getsigstr(struct rt_device *dev)
210 {
211         if (inb(io) & 2)        /* bit set = no signal present  */
212                 return 0;
213         return 1;               /* signal present               */
214 }
215
216 static int rt_do_ioctl(struct inode *inode, struct file *file,
217                        unsigned int cmd, void *arg)
218 {
219         struct video_device *dev = video_devdata(file);
220         struct rt_device *rt=dev->priv;
221         
222         switch(cmd)
223         {
224                 case VIDIOCGCAP:
225                 {
226                         struct video_capability *v = arg;
227                         memset(v,0,sizeof(*v));
228                         v->type=VID_TYPE_TUNER;
229                         v->channels=1;
230                         v->audios=1;
231                         strcpy(v->name, "RadioTrack");
232                         return 0;
233                 }
234                 case VIDIOCGTUNER:
235                 {
236                         struct video_tuner *v = arg;
237                         if(v->tuner)    /* Only 1 tuner */ 
238                                 return -EINVAL;
239                         v->rangelow=(87*16000);
240                         v->rangehigh=(108*16000);
241                         v->flags=VIDEO_TUNER_LOW;
242                         v->mode=VIDEO_MODE_AUTO;
243                         strcpy(v->name, "FM");
244                         v->signal=0xFFFF*rt_getsigstr(rt);
245                         return 0;
246                 }
247                 case VIDIOCSTUNER:
248                 {
249                         struct video_tuner *v = arg;
250                         if(v->tuner!=0)
251                                 return -EINVAL;
252                         /* Only 1 tuner so no setting needed ! */
253                         return 0;
254                 }
255                 case VIDIOCGFREQ:
256                 {
257                         unsigned long *freq = arg;
258                         *freq = rt->curfreq;
259                         return 0;
260                 }
261                 case VIDIOCSFREQ:
262                 {
263                         unsigned long *freq = arg;
264                         rt->curfreq = *freq;
265                         rt_setfreq(rt, rt->curfreq);
266                         return 0;
267                 }
268                 case VIDIOCGAUDIO:
269                 {       
270                         struct video_audio *v = arg;
271                         memset(v,0, sizeof(*v));
272                         v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
273                         v->volume=rt->curvol * 6554;
274                         v->step=6554;
275                         strcpy(v->name, "Radio");
276                         return 0;                       
277                 }
278                 case VIDIOCSAUDIO:
279                 {
280                         struct video_audio *v = arg;
281                         if(v->audio) 
282                                 return -EINVAL;
283                         if(v->flags&VIDEO_AUDIO_MUTE) 
284                                 rt_mute(rt);
285                         else
286                                 rt_setvol(rt,v->volume/6554);
287                         return 0;
288                 }
289                 default:
290                         return -ENOIOCTLCMD;
291         }
292 }
293
294 static int rt_ioctl(struct inode *inode, struct file *file,
295                     unsigned int cmd, unsigned long arg)
296 {
297         return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
298 }
299
300 static struct rt_device rtrack_unit;
301
302 static struct file_operations rtrack_fops = {
303         .owner          = THIS_MODULE,
304         .open           = video_exclusive_open,
305         .release        = video_exclusive_release,
306         .ioctl          = rt_ioctl,
307         .llseek         = no_llseek,
308 };
309
310 static struct video_device rtrack_radio=
311 {
312         .owner          = THIS_MODULE,
313         .name           = "RadioTrack radio",
314         .type           = VID_TYPE_TUNER,
315         .hardware       = VID_HARDWARE_RTRACK,
316         .fops           = &rtrack_fops,
317 };
318
319 static int __init rtrack_init(void)
320 {
321         if(io==-1)
322         {
323                 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
324                 return -EINVAL;
325         }
326
327         if (!request_region(io, 2, "rtrack")) 
328         {
329                 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
330                 return -EBUSY;
331         }
332
333         rtrack_radio.priv=&rtrack_unit;
334         
335         if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
336         {
337                 release_region(io, 2);
338                 return -EINVAL;
339         }
340         printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
341
342         /* Set up the I/O locking */
343         
344         init_MUTEX(&lock);
345         
346         /* mute card - prevents noisy bootups */
347
348         /* this ensures that the volume is all the way down  */
349         outb(0x48, io);         /* volume down but still "on"   */
350         sleep_delay(2000000);   /* make sure it's totally down  */
351         outb(0xc0, io);         /* steady volume, mute card     */
352         rtrack_unit.curvol = 0;
353
354         return 0;
355 }
356
357 MODULE_AUTHOR("M.Kirkwood");
358 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
359 MODULE_LICENSE("GPL");
360
361 MODULE_PARM(io, "i");
362 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
363 MODULE_PARM(radio_nr, "i");
364
365 static void __exit cleanup_rtrack_module(void)
366 {
367         video_unregister_device(&rtrack_radio);
368         release_region(io,2);
369 }
370
371 module_init(rtrack_init);
372 module_exit(cleanup_rtrack_module);
373