Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / 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_param(radio_nr, int, 0);
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, NULL, 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         pr_debug("check_sig: 0x%x\n", i);
79         if (i & 0x80) {
80                 /* no signal from tuner */
81                 *flags=0;
82                 *signal=0;
83                 return 0;
84         } else
85                 *signal=0xffff;
86
87         if ((i=aci_rw_cmd(ACI_READ_TUNERSTEREO, -1, -1))<0)
88                 return i;
89         if (i & 0x40) {
90                 *flags=0;
91         } else {
92                 /* stereo */
93                 *flags=VIDEO_TUNER_STEREO_ON;
94                 /* I can't see stereo, when forced to mono */
95                 dev->stereo=1;
96         }
97
98         if ((i=aci_rds_cmd(RDS_STATUS, &buf, 1))<0)
99                 return i;
100         if (buf & 1)
101                 /* RDS available */
102                 *flags|=VIDEO_TUNER_RDS_ON;
103         else
104                 return 0;
105
106         if ((i=aci_rds_cmd(RDS_RXVALUE, &buf, 1))<0)
107                 return i;
108         pr_debug("rds-signal: %d\n", buf);
109         if (buf > 15) {
110                 printk("miropcm20-radio: RX strengths unexpected high...\n");
111                 buf=15;
112         }
113         /* refine signal */
114         if ((*signal=SCALE(15, 0xffff, buf))==0)
115                 *signal = 1;
116
117         return 0;
118 }
119
120 static int pcm20_do_ioctl(struct inode *inode, struct file *file,
121                           unsigned int cmd, void *arg)
122 {
123         struct video_device *dev = video_devdata(file);
124         struct pcm20_device *pcm20 = dev->priv;
125         int i;
126         
127         switch(cmd)
128         {
129                 case VIDIOCGCAP:
130                 {
131                         struct video_capability *v = arg;
132                         memset(v,0,sizeof(*v));
133                         v->type=VID_TYPE_TUNER;
134                         strcpy(v->name, "Miro PCM20");
135                         v->channels=1;
136                         v->audios=1;
137                         return 0;
138                 }
139                 case VIDIOCGTUNER:
140                 {
141                         struct video_tuner *v = arg;
142                         if(v->tuner)    /* Only 1 tuner */ 
143                                 return -EINVAL;
144                         v->rangelow=87*16000;
145                         v->rangehigh=108*16000;
146                         pcm20_getflags(pcm20, &v->flags, &v->signal);
147                         v->flags|=VIDEO_TUNER_LOW;
148                         v->mode=VIDEO_MODE_AUTO;
149                         strcpy(v->name, "FM");
150                         return 0;
151                 }
152                 case VIDIOCSTUNER:
153                 {
154                         struct video_tuner *v = arg;
155                         if(v->tuner!=0)
156                                 return -EINVAL;
157                         /* Only 1 tuner so no setting needed ! */
158                         return 0;
159                 }
160                 case VIDIOCGFREQ:
161                 {
162                         unsigned long *freq = arg;
163                         *freq = pcm20->freq;
164                         return 0;
165                 }
166                 case VIDIOCSFREQ:
167                 {
168                         unsigned long *freq = arg;
169                         pcm20->freq = *freq;
170                         i=pcm20_setfreq(pcm20, pcm20->freq);
171                         pr_debug("First view (setfreq): 0x%x\n", i);
172                         return i;
173                 }
174                 case VIDIOCGAUDIO:
175                 {       
176                         struct video_audio *v = arg;
177                         memset(v,0, sizeof(*v));
178                         v->flags=VIDEO_AUDIO_MUTABLE;
179                         if (pcm20->muted)
180                                 v->flags|=VIDEO_AUDIO_MUTE;
181                         v->mode=VIDEO_SOUND_STEREO;
182                         if (pcm20->stereo)
183                                 v->mode|=VIDEO_SOUND_MONO;
184                         /* v->step=2048; */
185                         strcpy(v->name, "Radio");
186                         return 0;                       
187                 }
188                 case VIDIOCSAUDIO:
189                 {
190                         struct video_audio *v = arg;
191                         if(v->audio) 
192                                 return -EINVAL;
193
194                         pcm20_mute(pcm20, !!(v->flags&VIDEO_AUDIO_MUTE));
195                         if(v->flags&VIDEO_SOUND_MONO)
196                                 pcm20_stereo(pcm20, 0);
197                         if(v->flags&VIDEO_SOUND_STEREO)
198                                 pcm20_stereo(pcm20, 1);
199
200                         return 0;
201                 }
202                 default:
203                         return -ENOIOCTLCMD;
204         }
205 }
206
207 static int pcm20_ioctl(struct inode *inode, struct file *file,
208                        unsigned int cmd, unsigned long arg)
209 {
210         return video_usercopy(inode, file, cmd, arg, pcm20_do_ioctl);
211 }
212
213 static struct pcm20_device pcm20_unit = {
214         .freq   = 87*16000,
215         .muted  = 1,
216 };
217
218 static struct file_operations pcm20_fops = {
219         .owner          = THIS_MODULE,
220         .open           = video_exclusive_open,
221         .release        = video_exclusive_release,
222         .ioctl          = pcm20_ioctl,
223         .compat_ioctl   = v4l_compat_ioctl32,
224         .llseek         = no_llseek,
225 };
226
227 static struct video_device pcm20_radio = {
228         .owner          = THIS_MODULE,
229         .name           = "Miro PCM 20 radio",
230         .type           = VID_TYPE_TUNER,
231         .hardware       = VID_HARDWARE_RTRACK,
232         .fops           = &pcm20_fops,
233         .priv           = &pcm20_unit
234 };
235
236 static int __init pcm20_init(void)
237 {
238         if(video_register_device(&pcm20_radio, VFL_TYPE_RADIO, radio_nr)==-1)
239                 goto video_register_device;
240                 
241         if(attach_aci_rds()<0)
242                 goto attach_aci_rds;
243
244         printk(KERN_INFO "Miro PCM20 radio card driver.\n");
245
246         return 0;
247
248  attach_aci_rds:
249         video_unregister_device(&pcm20_radio);
250  video_register_device:
251         return -EINVAL;
252 }
253
254 MODULE_AUTHOR("Ruurd Reitsma");
255 MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
256 MODULE_LICENSE("GPL");
257
258 static void __exit pcm20_cleanup(void)
259 {
260         unload_aci_rds();
261         video_unregister_device(&pcm20_radio);
262 }
263
264 module_init(pcm20_init);
265 module_exit(pcm20_cleanup);