ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / radio / radio-rtrack2.c
1 /* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
2  * 
3  * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
4  * Converted to new API by Alan Cox <Alan.Cox@linux.org>
5  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
6  *
7  * TODO: Allow for more than one of these foolish entities :-)
8  *
9  */
10
11 #include <linux/module.h>       /* Modules                      */
12 #include <linux/init.h>         /* Initdata                     */
13 #include <linux/ioport.h>       /* check_region, request_region */
14 #include <linux/delay.h>        /* udelay                       */
15 #include <asm/io.h>             /* outb, outb_p                 */
16 #include <asm/uaccess.h>        /* copy to/from user            */
17 #include <linux/videodev.h>     /* kernel radio structs         */
18 #include <linux/config.h>       /* CONFIG_RADIO_RTRACK2_PORT    */
19 #include <linux/spinlock.h>
20
21 #ifndef CONFIG_RADIO_RTRACK2_PORT
22 #define CONFIG_RADIO_RTRACK2_PORT -1
23 #endif
24
25 static int io = CONFIG_RADIO_RTRACK2_PORT; 
26 static int radio_nr = -1;
27 static spinlock_t lock;
28
29 struct rt_device
30 {
31         int port;
32         unsigned long curfreq;
33         int muted;
34 };
35
36
37 /* local things */
38
39 static void rt_mute(struct rt_device *dev)
40 {
41         if(dev->muted)
42                 return;
43         spin_lock(&lock);
44         outb(1, io);
45         spin_unlock(&lock);
46         dev->muted = 1;
47 }
48
49 static void rt_unmute(struct rt_device *dev)
50 {
51         if(dev->muted == 0)
52                 return;
53         spin_lock(&lock);
54         outb(0, io);
55         spin_unlock(&lock);
56         dev->muted = 0;
57 }
58
59 static void zero(void)
60 {
61         outb_p(1, io);
62         outb_p(3, io);
63         outb_p(1, io);
64 }
65
66 static void one(void)
67 {
68         outb_p(5, io);
69         outb_p(7, io);
70         outb_p(5, io);
71 }
72
73 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
74 {
75         int i;
76
77         freq = freq / 200 + 856;
78         
79         spin_lock(&lock);
80
81         outb_p(0xc8, io);
82         outb_p(0xc9, io);
83         outb_p(0xc9, io);
84
85         for (i = 0; i < 10; i++)
86                 zero ();
87
88         for (i = 14; i >= 0; i--)
89                 if (freq & (1 << i))
90                         one ();
91                 else
92                         zero ();
93
94         outb_p(0xc8, io);
95         if (!dev->muted)
96                 outb_p(0, io);
97                 
98         spin_unlock(&lock);
99         return 0;
100 }
101
102 static int rt_getsigstr(struct rt_device *dev)
103 {
104         if (inb(io) & 2)        /* bit set = no signal present  */
105                 return 0;
106         return 1;               /* signal present               */
107 }
108
109 static int rt_do_ioctl(struct inode *inode, struct file *file,
110                        unsigned int cmd, void *arg)
111 {
112         struct video_device *dev = video_devdata(file);
113         struct rt_device *rt=dev->priv;
114
115         switch(cmd)
116         {
117                 case VIDIOCGCAP:
118                 {
119                         struct video_capability *v = arg;
120                         memset(v,0,sizeof(*v));
121                         v->type=VID_TYPE_TUNER;
122                         v->channels=1;
123                         v->audios=1;
124                         strcpy(v->name, "RadioTrack II");
125                         return 0;
126                 }
127                 case VIDIOCGTUNER:
128                 {
129                         struct video_tuner *v = arg;
130                         if(v->tuner)    /* Only 1 tuner */ 
131                                 return -EINVAL;
132                         v->rangelow=88*16000;
133                         v->rangehigh=108*16000;
134                         v->flags=VIDEO_TUNER_LOW;
135                         v->mode=VIDEO_MODE_AUTO;
136                         v->signal=0xFFFF*rt_getsigstr(rt);
137                         strcpy(v->name, "FM");
138                         return 0;
139                 }
140                 case VIDIOCSTUNER:
141                 {
142                         struct video_tuner *v = arg;
143                         if(v->tuner!=0)
144                                 return -EINVAL;
145                         /* Only 1 tuner so no setting needed ! */
146                         return 0;
147                 }
148                 case VIDIOCGFREQ:
149                 {
150                         unsigned long *freq = arg;
151                         *freq = rt->curfreq;
152                         return 0;
153                 }
154                 case VIDIOCSFREQ:
155                 {
156                         unsigned long *freq = arg;
157                         rt->curfreq = *freq;
158                         rt_setfreq(rt, rt->curfreq);
159                         return 0;
160                 }
161                 case VIDIOCGAUDIO:
162                 {       
163                         struct video_audio *v = arg;
164                         memset(v,0, sizeof(*v));
165                         v->flags|=VIDEO_AUDIO_MUTABLE;
166                         v->volume=1;
167                         v->step=65535;
168                         strcpy(v->name, "Radio");
169                         return 0;                       
170                 }
171                 case VIDIOCSAUDIO:
172                 {
173                         struct video_audio *v = arg;
174                         if(v->audio) 
175                                 return -EINVAL;
176
177                         if(v->flags&VIDEO_AUDIO_MUTE) 
178                                 rt_mute(rt);
179                         else
180                                 rt_unmute(rt);
181
182                         return 0;
183                 }
184                 default:
185                         return -ENOIOCTLCMD;
186         }
187 }
188
189 static int rt_ioctl(struct inode *inode, struct file *file,
190                     unsigned int cmd, unsigned long arg)
191 {
192         return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
193 }
194
195 static struct rt_device rtrack2_unit;
196
197 static struct file_operations rtrack2_fops = {
198         .owner          = THIS_MODULE,
199         .open           = video_exclusive_open,
200         .release        = video_exclusive_release,
201         .ioctl          = rt_ioctl,
202         .llseek         = no_llseek,
203 };
204
205 static struct video_device rtrack2_radio=
206 {
207         .owner          = THIS_MODULE,
208         .name           = "RadioTrack II radio",
209         .type           = VID_TYPE_TUNER,
210         .hardware       = VID_HARDWARE_RTRACK2,
211         .fops           = &rtrack2_fops,
212 };
213
214 static int __init rtrack2_init(void)
215 {
216         if(io==-1)
217         {
218                 printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
219                 return -EINVAL;
220         }
221         if (!request_region(io, 4, "rtrack2")) 
222         {
223                 printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
224                 return -EBUSY;
225         }
226
227         rtrack2_radio.priv=&rtrack2_unit;
228
229         spin_lock_init(&lock);  
230         if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
231         {
232                 release_region(io, 4);
233                 return -EINVAL;
234         }
235                 
236         printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
237
238         /* mute card - prevents noisy bootups */
239         outb(1, io);
240         rtrack2_unit.muted = 1;
241
242         return 0;
243 }
244
245 MODULE_AUTHOR("Ben Pfaff");
246 MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
247 MODULE_LICENSE("GPL");
248
249 MODULE_PARM(io, "i");
250 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
251 MODULE_PARM(radio_nr, "i");
252
253 static void __exit rtrack2_cleanup_module(void)
254 {
255         video_unregister_device(&rtrack2_radio);
256         release_region(io,4);
257 }
258
259 module_init(rtrack2_init);
260 module_exit(rtrack2_cleanup_module);
261
262 /*
263   Local variables:
264   compile-command: "mmake"
265   End:
266 */