Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[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 <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
27 #include "oss/aci.h"
28 #include "miropcm20-rds-core.h"
29
30 static int radio_nr = -1;
31 module_param(radio_nr, int, 0);
32
33 struct pcm20_device {
34         unsigned long freq;
35         int muted;
36         int stereo;
37 };
38
39
40 static int pcm20_mute(struct pcm20_device *dev, unsigned char mute)
41 {
42         dev->muted = mute;
43         return aci_write_cmd(ACI_SET_TUNERMUTE, mute);
44 }
45
46 static int pcm20_stereo(struct pcm20_device *dev, unsigned char stereo)
47 {
48         dev->stereo = stereo;
49         return aci_write_cmd(ACI_SET_TUNERMONO, !stereo);
50 }
51
52 static int pcm20_setfreq(struct pcm20_device *dev, unsigned long freq)
53 {
54         unsigned char freql;
55         unsigned char freqh;
56
57         dev->freq=freq;
58
59         freq /= 160;
60         if (!(aci_version==0x07 || aci_version>=0xb0))
61                 freq /= 10;  /* I don't know exactly which version
62                               * needs this hack */
63         freql = freq & 0xff;
64         freqh = freq >> 8;
65
66         aci_rds_cmd(RDS_RESET, NULL, 0);
67         pcm20_stereo(dev, 1);
68
69         return aci_rw_cmd(ACI_WRITE_TUNE, freql, freqh);
70 }
71
72 static int pcm20_getflags(struct pcm20_device *dev, __u32 *flags, __u16 *signal)
73 {
74         /* okay, check for signal, stereo and rds here... */
75         int i;
76         unsigned char buf;
77
78         if ((i=aci_rw_cmd(ACI_READ_TUNERSTATION, -1, -1))<0)
79                 return i;
80         pr_debug("check_sig: 0x%x\n", i);
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         pr_debug("rds-signal: %d\n", buf);
111         if (buf > 15) {
112                 printk("miropcm20-radio: RX strengths unexpected high...\n");
113                 buf=15;
114         }
115         /* refine signal */
116         if ((*signal=SCALE(15, 0xffff, buf))==0)
117                 *signal = 1;
118
119         return 0;
120 }
121
122 static int pcm20_do_ioctl(struct inode *inode, struct file *file,
123                           unsigned int cmd, void *arg)
124 {
125         struct video_device *dev = video_devdata(file);
126         struct pcm20_device *pcm20 = dev->priv;
127         int i;
128
129         switch(cmd)
130         {
131                 case VIDIOCGCAP:
132                 {
133                         struct video_capability *v = arg;
134                         memset(v,0,sizeof(*v));
135                         v->type=VID_TYPE_TUNER;
136                         strcpy(v->name, "Miro PCM20");
137                         v->channels=1;
138                         v->audios=1;
139                         return 0;
140                 }
141                 case VIDIOCGTUNER:
142                 {
143                         struct video_tuner *v = arg;
144                         if(v->tuner)    /* Only 1 tuner */
145                                 return -EINVAL;
146                         v->rangelow=87*16000;
147                         v->rangehigh=108*16000;
148                         pcm20_getflags(pcm20, &v->flags, &v->signal);
149                         v->flags|=VIDEO_TUNER_LOW;
150                         v->mode=VIDEO_MODE_AUTO;
151                         strcpy(v->name, "FM");
152                         return 0;
153                 }
154                 case VIDIOCSTUNER:
155                 {
156                         struct video_tuner *v = arg;
157                         if(v->tuner!=0)
158                                 return -EINVAL;
159                         /* Only 1 tuner so no setting needed ! */
160                         return 0;
161                 }
162                 case VIDIOCGFREQ:
163                 {
164                         unsigned long *freq = arg;
165                         *freq = pcm20->freq;
166                         return 0;
167                 }
168                 case VIDIOCSFREQ:
169                 {
170                         unsigned long *freq = arg;
171                         pcm20->freq = *freq;
172                         i=pcm20_setfreq(pcm20, pcm20->freq);
173                         pr_debug("First view (setfreq): 0x%x\n", i);
174                         return i;
175                 }
176                 case VIDIOCGAUDIO:
177                 {
178                         struct video_audio *v = arg;
179                         memset(v,0, sizeof(*v));
180                         v->flags=VIDEO_AUDIO_MUTABLE;
181                         if (pcm20->muted)
182                                 v->flags|=VIDEO_AUDIO_MUTE;
183                         v->mode=VIDEO_SOUND_STEREO;
184                         if (pcm20->stereo)
185                                 v->mode|=VIDEO_SOUND_MONO;
186                         /* v->step=2048; */
187                         strcpy(v->name, "Radio");
188                         return 0;
189                 }
190                 case VIDIOCSAUDIO:
191                 {
192                         struct video_audio *v = arg;
193                         if(v->audio)
194                                 return -EINVAL;
195
196                         pcm20_mute(pcm20, !!(v->flags&VIDEO_AUDIO_MUTE));
197                         if(v->flags&VIDEO_SOUND_MONO)
198                                 pcm20_stereo(pcm20, 0);
199                         if(v->flags&VIDEO_SOUND_STEREO)
200                                 pcm20_stereo(pcm20, 1);
201
202                         return 0;
203                 }
204                 default:
205                         return -ENOIOCTLCMD;
206         }
207 }
208
209 static int pcm20_ioctl(struct inode *inode, struct file *file,
210                        unsigned int cmd, unsigned long arg)
211 {
212         return video_usercopy(inode, file, cmd, arg, pcm20_do_ioctl);
213 }
214
215 static struct pcm20_device pcm20_unit = {
216         .freq   = 87*16000,
217         .muted  = 1,
218 };
219
220 static const struct file_operations pcm20_fops = {
221         .owner          = THIS_MODULE,
222         .open           = video_exclusive_open,
223         .release        = video_exclusive_release,
224         .ioctl          = pcm20_ioctl,
225 #ifdef CONFIG_COMPAT
226         .compat_ioctl   = v4l_compat_ioctl32,
227 #endif
228         .llseek         = no_llseek,
229 };
230
231 static struct video_device pcm20_radio = {
232         .name           = "Miro PCM 20 radio",
233         .fops           = &pcm20_fops,
234         .priv           = &pcm20_unit
235 };
236
237 static int __init pcm20_init(void)
238 {
239         if(video_register_device(&pcm20_radio, VFL_TYPE_RADIO, radio_nr)==-1)
240                 goto video_register_device;
241
242         if(attach_aci_rds()<0)
243                 goto attach_aci_rds;
244
245         printk(KERN_INFO "Miro PCM20 radio card driver.\n");
246
247         return 0;
248
249  attach_aci_rds:
250         video_unregister_device(&pcm20_radio);
251  video_register_device:
252         return -EINVAL;
253 }
254
255 MODULE_AUTHOR("Ruurd Reitsma");
256 MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
257 MODULE_LICENSE("GPL");
258
259 static void __exit pcm20_cleanup(void)
260 {
261         unload_aci_rds();
262         video_unregister_device(&pcm20_radio);
263 }
264
265 module_init(pcm20_init);
266 module_exit(pcm20_cleanup);