ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / radio / miropcm20-radio.c
1 /* Miro PCM20 radio driver for Linux radio support
2  * (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
3  * Thanks to Norberto Pellici for the ACI device interface specification
4  * The API part is based on the radiotrack driver by M. Kirkwood
5  * This driver relies on the aci mixer (drivers/sound/aci.c)
6  * Look there for further info...
7  */
8
9 /* Revision history:
10  *
11  *   1998        Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
12  *   2000-09-05  Robert Siemer <Robert.Siemer@gmx.de>
13  *        removed unfinished volume control (maybe adding it later again)
14  *        use OSS-mixer; added stereo control
15  */
16
17 /* What ever you think about the ACI, version 0x07 is not very well!
18  * I can't get frequency, 'tuner status', 'tuner flags' or mute/mono
19  * conditions...                Robert 
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/videodev.h>
25 #include "../../../sound/oss/aci.h"
26 #include "miropcm20-rds-core.h"
27
28 static int radio_nr = -1;
29 MODULE_PARM(radio_nr, "i");
30
31 struct pcm20_device {
32         unsigned long freq;
33         int muted;
34         int stereo;
35 };
36
37
38 static int pcm20_mute(struct pcm20_device *dev, unsigned char mute)
39 {
40         dev->muted = mute;
41         return aci_write_cmd(ACI_SET_TUNERMUTE, mute);
42 }
43
44 static int pcm20_stereo(struct pcm20_device *dev, unsigned char stereo)
45 {
46         dev->stereo = stereo;
47         return aci_write_cmd(ACI_SET_TUNERMONO, !stereo);
48 }
49
50 static int pcm20_setfreq(struct pcm20_device *dev, unsigned long freq)
51 {
52         unsigned char freql;
53         unsigned char freqh;
54
55         dev->freq=freq;
56
57         freq /= 160;
58         if (!(aci_version==0x07 || aci_version>=0xb0))
59                 freq /= 10;  /* I don't know exactly which version
60                               * needs this hack */
61         freql = freq & 0xff;
62         freqh = freq >> 8;
63
64         aci_rds_cmd(RDS_RESET, 0, 0);
65         pcm20_stereo(dev, 1);
66
67         return aci_rw_cmd(ACI_WRITE_TUNE, freql, freqh);
68 }
69
70 static int pcm20_getflags(struct pcm20_device *dev, __u32 *flags, __u16 *signal)
71 {
72         /* okay, check for signal, stereo and rds here... */
73         int i;
74         unsigned char buf;
75
76         if ((i=aci_rw_cmd(ACI_READ_TUNERSTATION, -1, -1))<0)
77                 return i;
78 #if DEBUG
79         printk("check_sig: 0x%x\n", i);
80 #endif
81         if (i & 0x80) {
82                 /* no signal from tuner */
83                 *flags=0;
84                 *signal=0;
85                 return 0;
86         } else
87                 *signal=0xffff;
88
89         if ((i=aci_rw_cmd(ACI_READ_TUNERSTEREO, -1, -1))<0)
90                 return i;
91         if (i & 0x40) {
92                 *flags=0;
93         } else {
94                 /* stereo */
95                 *flags=VIDEO_TUNER_STEREO_ON;
96                 /* I can't see stereo, when forced to mono */
97                 dev->stereo=1;
98         }
99
100         if ((i=aci_rds_cmd(RDS_STATUS, &buf, 1))<0)
101                 return i;
102         if (buf & 1)
103                 /* RDS available */
104                 *flags|=VIDEO_TUNER_RDS_ON;
105         else
106                 return 0;
107
108         if ((i=aci_rds_cmd(RDS_RXVALUE, &buf, 1))<0)
109                 return i;
110 #if DEBUG
111         printk("rds-signal: %d\n", buf);
112 #endif
113         if (buf > 15) {
114                 printk("miropcm20-radio: RX strengths unexpected high...\n");
115                 buf=15;
116         }
117         /* refine signal */
118         if ((*signal=SCALE(15, 0xffff, buf))==0)
119                 *signal = 1;
120
121         return 0;
122 }
123
124 static int pcm20_do_ioctl(struct inode *inode, struct file *file,
125                           unsigned int cmd, void *arg)
126 {
127         struct video_device *dev = video_devdata(file);
128         struct pcm20_device *pcm20 = dev->priv;
129         int i;
130         
131         switch(cmd)
132         {
133                 case VIDIOCGCAP:
134                 {
135                         struct video_capability *v = arg;
136                         memset(v,0,sizeof(*v));
137                         v->type=VID_TYPE_TUNER;
138                         strcpy(v->name, "Miro PCM20");
139                         v->channels=1;
140                         v->audios=1;
141                         return 0;
142                 }
143                 case VIDIOCGTUNER:
144                 {
145                         struct video_tuner *v = arg;
146                         if(v->tuner)    /* Only 1 tuner */ 
147                                 return -EINVAL;
148                         v->rangelow=87*16000;
149                         v->rangehigh=108*16000;
150                         pcm20_getflags(pcm20, &v->flags, &v->signal);
151                         v->flags|=VIDEO_TUNER_LOW;
152                         v->mode=VIDEO_MODE_AUTO;
153                         strcpy(v->name, "FM");
154                         return 0;
155                 }
156                 case VIDIOCSTUNER:
157                 {
158                         struct video_tuner *v = arg;
159                         if(v->tuner!=0)
160                                 return -EINVAL;
161                         /* Only 1 tuner so no setting needed ! */
162                         return 0;
163                 }
164                 case VIDIOCGFREQ:
165                 {
166                         unsigned long *freq = arg;
167                         *freq = pcm20->freq;
168                         return 0;
169                 }
170                 case VIDIOCSFREQ:
171                 {
172                         unsigned long *freq = arg;
173                         pcm20->freq = *freq;
174                         i=pcm20_setfreq(pcm20, pcm20->freq);
175 #if DEBUG
176                         printk("First view (setfreq): 0x%x\n", i);
177 #endif
178                         return i;
179                 }
180                 case VIDIOCGAUDIO:
181                 {       
182                         struct video_audio *v = arg;
183                         memset(v,0, sizeof(*v));
184                         v->flags=VIDEO_AUDIO_MUTABLE;
185                         if (pcm20->muted)
186                                 v->flags|=VIDEO_AUDIO_MUTE;
187                         v->mode=VIDEO_SOUND_STEREO;
188                         if (pcm20->stereo)
189                                 v->mode|=VIDEO_SOUND_MONO;
190                         /* v->step=2048; */
191                         strcpy(v->name, "Radio");
192                         return 0;                       
193                 }
194                 case VIDIOCSAUDIO:
195                 {
196                         struct video_audio *v = arg;
197                         if(v->audio) 
198                                 return -EINVAL;
199
200                         pcm20_mute(pcm20, !!(v->flags&VIDEO_AUDIO_MUTE));
201                         if(v->flags&VIDEO_SOUND_MONO)
202                                 pcm20_stereo(pcm20, 0);
203                         if(v->flags&VIDEO_SOUND_STEREO)
204                                 pcm20_stereo(pcm20, 1);
205
206                         return 0;
207                 }
208                 default:
209                         return -ENOIOCTLCMD;
210         }
211 }
212
213 static int pcm20_ioctl(struct inode *inode, struct file *file,
214                        unsigned int cmd, unsigned long arg)
215 {
216         return video_usercopy(inode, file, cmd, arg, pcm20_do_ioctl);
217 }
218
219 static struct pcm20_device pcm20_unit = {
220         .freq   = 87*16000,
221         .muted  = 1,
222 };
223
224 static struct file_operations pcm20_fops = {
225         .owner          = THIS_MODULE,
226         .open           = video_exclusive_open,
227         .release        = video_exclusive_release,
228         .ioctl          = pcm20_ioctl,
229         .llseek         = no_llseek,
230 };
231
232 static struct video_device pcm20_radio = {
233         .owner          = THIS_MODULE,
234         .name           = "Miro PCM 20 radio",
235         .type           = VID_TYPE_TUNER,
236         .hardware       = VID_HARDWARE_RTRACK,
237         .fops           = &pcm20_fops,
238         .priv           = &pcm20_unit
239 };
240
241 static int __init pcm20_init(void)
242 {
243         if(video_register_device(&pcm20_radio, VFL_TYPE_RADIO, radio_nr)==-1)
244                 goto video_register_device;
245                 
246         if(attach_aci_rds()<0)
247                 goto attach_aci_rds;
248
249         printk(KERN_INFO "Miro PCM20 radio card driver.\n");
250
251         return 0;
252
253  attach_aci_rds:
254         video_unregister_device(&pcm20_radio);
255  video_register_device:
256         return -EINVAL;
257 }
258
259 MODULE_AUTHOR("Ruurd Reitsma");
260 MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
261 MODULE_LICENSE("GPL");
262
263 static void __exit pcm20_cleanup(void)
264 {
265         unload_aci_rds();
266         video_unregister_device(&pcm20_radio);
267 }
268
269 module_init(pcm20_init);
270 module_exit(pcm20_cleanup);