Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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>       /* 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 mutex 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                 msleep(jiffies_to_msecs(d));
67 }
68
69 static void rt_decvol(void)
70 {
71         outb(0x58, io);         /* volume down + sigstr + on    */
72         sleep_delay(100000);
73         outb(0xd8, io);         /* volume steady + sigstr + on  */
74 }
75
76 static void rt_incvol(void)
77 {
78         outb(0x98, io);         /* volume up + sigstr + on      */
79         sleep_delay(100000);
80         outb(0xd8, io);         /* volume steady + sigstr + on  */
81 }
82
83 static void rt_mute(struct rt_device *dev)
84 {
85         dev->muted = 1;
86         mutex_lock(&lock);
87         outb(0xd0, io);                 /* volume steady, off           */
88         mutex_unlock(&lock);
89 }
90
91 static int rt_setvol(struct rt_device *dev, int vol)
92 {
93         int i;
94
95         mutex_lock(&lock);
96         
97         if(vol == dev->curvol) {        /* requested volume = current */
98                 if (dev->muted) {       /* user is unmuting the card  */
99                         dev->muted = 0;
100                         outb (0xd8, io);        /* enable card */
101                 }       
102                 mutex_unlock(&lock);
103                 return 0;
104         }
105
106         if(vol == 0) {                  /* volume = 0 means mute the card */
107                 outb(0x48, io);         /* volume down but still "on"   */
108                 sleep_delay(2000000);   /* make sure it's totally down  */
109                 outb(0xd0, io);         /* volume steady, off           */
110                 dev->curvol = 0;        /* track the volume state!      */
111                 mutex_unlock(&lock);
112                 return 0;
113         }
114
115         dev->muted = 0;
116         if(vol > dev->curvol)
117                 for(i = dev->curvol; i < vol; i++) 
118                         rt_incvol();
119         else
120                 for(i = dev->curvol; i > vol; i--) 
121                         rt_decvol();
122
123         dev->curvol = vol;
124         mutex_unlock(&lock);
125         return 0;
126 }
127
128 /* the 128+64 on these outb's is to keep the volume stable while tuning 
129  * without them, the volume _will_ creep up with each frequency change
130  * and bit 4 (+16) is to keep the signal strength meter enabled
131  */
132
133 static void send_0_byte(int port, struct rt_device *dev)
134 {
135         if ((dev->curvol == 0) || (dev->muted)) {
136                 outb_p(128+64+16+  1, port);   /* wr-enable + data low */
137                 outb_p(128+64+16+2+1, port);   /* clock */
138         }
139         else {
140                 outb_p(128+64+16+8+  1, port);  /* on + wr-enable + data low */
141                 outb_p(128+64+16+8+2+1, port);  /* clock */
142         }
143         sleep_delay(1000); 
144 }
145
146 static void send_1_byte(int port, struct rt_device *dev)
147 {
148         if ((dev->curvol == 0) || (dev->muted)) {
149                 outb_p(128+64+16+4  +1, port);   /* wr-enable+data high */
150                 outb_p(128+64+16+4+2+1, port);   /* clock */
151         } 
152         else {
153                 outb_p(128+64+16+8+4  +1, port); /* on+wr-enable+data high */
154                 outb_p(128+64+16+8+4+2+1, port); /* clock */
155         }
156
157         sleep_delay(1000); 
158 }
159
160 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
161 {
162         int i;
163
164         /* adapted from radio-aztech.c */
165
166         /* now uses VIDEO_TUNER_LOW for fine tuning */
167
168         freq += 171200;                 /* Add 10.7 MHz IF              */
169         freq /= 800;                    /* Convert to 50 kHz units      */
170         
171         mutex_lock(&lock);                      /* Stop other ops interfering */
172          
173         send_0_byte (io, dev);          /*  0: LSB of frequency         */
174
175         for (i = 0; i < 13; i++)        /*   : frequency bits (1-13)    */
176                 if (freq & (1 << i))
177                         send_1_byte (io, dev);
178                 else
179                         send_0_byte (io, dev);
180
181         send_0_byte (io, dev);          /* 14: test bit - always 0    */
182         send_0_byte (io, dev);          /* 15: test bit - always 0    */
183
184         send_0_byte (io, dev);          /* 16: band data 0 - always 0 */
185         send_0_byte (io, dev);          /* 17: band data 1 - always 0 */
186         send_0_byte (io, dev);          /* 18: band data 2 - always 0 */
187         send_0_byte (io, dev);          /* 19: time base - always 0   */
188
189         send_0_byte (io, dev);          /* 20: spacing (0 = 25 kHz)   */
190         send_1_byte (io, dev);          /* 21: spacing (1 = 25 kHz)   */
191         send_0_byte (io, dev);          /* 22: spacing (0 = 25 kHz)   */
192         send_1_byte (io, dev);          /* 23: AM/FM (FM = 1, always) */
193
194         if ((dev->curvol == 0) || (dev->muted))
195                 outb (0xd0, io);        /* volume steady + sigstr */
196         else
197                 outb (0xd8, io);        /* volume steady + sigstr + on */
198                 
199         mutex_unlock(&lock);
200
201         return 0;
202 }
203
204 static int rt_getsigstr(struct rt_device *dev)
205 {
206         if (inb(io) & 2)        /* bit set = no signal present  */
207                 return 0;
208         return 1;               /* signal present               */
209 }
210
211 static int rt_do_ioctl(struct inode *inode, struct file *file,
212                        unsigned int cmd, void *arg)
213 {
214         struct video_device *dev = video_devdata(file);
215         struct rt_device *rt=dev->priv;
216         
217         switch(cmd)
218         {
219                 case VIDIOCGCAP:
220                 {
221                         struct video_capability *v = arg;
222                         memset(v,0,sizeof(*v));
223                         v->type=VID_TYPE_TUNER;
224                         v->channels=1;
225                         v->audios=1;
226                         strcpy(v->name, "RadioTrack");
227                         return 0;
228                 }
229                 case VIDIOCGTUNER:
230                 {
231                         struct video_tuner *v = arg;
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                         return 0;
241                 }
242                 case VIDIOCSTUNER:
243                 {
244                         struct video_tuner *v = arg;
245                         if(v->tuner!=0)
246                                 return -EINVAL;
247                         /* Only 1 tuner so no setting needed ! */
248                         return 0;
249                 }
250                 case VIDIOCGFREQ:
251                 {
252                         unsigned long *freq = arg;
253                         *freq = rt->curfreq;
254                         return 0;
255                 }
256                 case VIDIOCSFREQ:
257                 {
258                         unsigned long *freq = arg;
259                         rt->curfreq = *freq;
260                         rt_setfreq(rt, rt->curfreq);
261                         return 0;
262                 }
263                 case VIDIOCGAUDIO:
264                 {       
265                         struct video_audio *v = arg;
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                         return 0;                       
272                 }
273                 case VIDIOCSAUDIO:
274                 {
275                         struct video_audio *v = arg;
276                         if(v->audio) 
277                                 return -EINVAL;
278                         if(v->flags&VIDEO_AUDIO_MUTE) 
279                                 rt_mute(rt);
280                         else
281                                 rt_setvol(rt,v->volume/6554);
282                         return 0;
283                 }
284                 default:
285                         return -ENOIOCTLCMD;
286         }
287 }
288
289 static int rt_ioctl(struct inode *inode, struct file *file,
290                     unsigned int cmd, unsigned long arg)
291 {
292         return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
293 }
294
295 static struct rt_device rtrack_unit;
296
297 static struct file_operations rtrack_fops = {
298         .owner          = THIS_MODULE,
299         .open           = video_exclusive_open,
300         .release        = video_exclusive_release,
301         .ioctl          = rt_ioctl,
302         .compat_ioctl   = v4l_compat_ioctl32,
303         .llseek         = no_llseek,
304 };
305
306 static struct video_device rtrack_radio=
307 {
308         .owner          = THIS_MODULE,
309         .name           = "RadioTrack radio",
310         .type           = VID_TYPE_TUNER,
311         .hardware       = VID_HARDWARE_RTRACK,
312         .fops           = &rtrack_fops,
313 };
314
315 static int __init rtrack_init(void)
316 {
317         if(io==-1)
318         {
319                 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
320                 return -EINVAL;
321         }
322
323         if (!request_region(io, 2, "rtrack")) 
324         {
325                 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
326                 return -EBUSY;
327         }
328
329         rtrack_radio.priv=&rtrack_unit;
330         
331         if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
332         {
333                 release_region(io, 2);
334                 return -EINVAL;
335         }
336         printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
337
338         /* Set up the I/O locking */
339         
340         mutex_init(&lock);
341         
342         /* mute card - prevents noisy bootups */
343
344         /* this ensures that the volume is all the way down  */
345         outb(0x48, io);         /* volume down but still "on"   */
346         sleep_delay(2000000);   /* make sure it's totally down  */
347         outb(0xc0, io);         /* steady volume, mute card     */
348         rtrack_unit.curvol = 0;
349
350         return 0;
351 }
352
353 MODULE_AUTHOR("M.Kirkwood");
354 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
355 MODULE_LICENSE("GPL");
356
357 module_param(io, int, 0);
358 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
359 module_param(radio_nr, int, 0);
360
361 static void __exit cleanup_rtrack_module(void)
362 {
363         video_unregister_device(&rtrack_radio);
364         release_region(io,2);
365 }
366
367 module_init(rtrack_init);
368 module_exit(cleanup_rtrack_module);
369