5b62285ed0ce7d9dddbb8f1c397ac8cd767154dc
[safe/jmp/linux-2.6] / drivers / media / radio / radio-sf16fmi.c
1 /* SF16FMI radio driver for Linux radio support
2  * heavily based on rtrack driver...
3  * (c) 1997 M. Kirkwood
4  * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
5  *
6  * Fitted to new interface by Alan Cox <alan.cox@linux.org>
7  * Made working and cleaned up functions <mikael.hedin@irf.se>
8  * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
9  *
10  * Notes on the hardware
11  *
12  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
13  *  No volume control - only mute/unmute - you have to use line volume
14  *  control on SB-part of SF16FMI
15  *
16  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17  */
18
19 #include <linux/kernel.h>       /* __setup                      */
20 #include <linux/module.h>       /* Modules                      */
21 #include <linux/init.h>         /* Initdata                     */
22 #include <linux/ioport.h>       /* request_region               */
23 #include <linux/delay.h>        /* udelay                       */
24 #include <linux/videodev2.h>    /* kernel radio structs         */
25 #include <media/v4l2-common.h>
26 #include <linux/isapnp.h>
27 #include <asm/io.h>             /* outb, outb_p                 */
28 #include <asm/uaccess.h>        /* copy to/from user            */
29 #include <linux/mutex.h>
30
31 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
32
33 static struct v4l2_queryctrl radio_qctrl[] = {
34         {
35                 .id            = V4L2_CID_AUDIO_MUTE,
36                 .name          = "Mute",
37                 .minimum       = 0,
38                 .maximum       = 1,
39                 .default_value = 1,
40                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
41         }
42 };
43
44 struct fmi_device
45 {
46         int port;
47         int curvol; /* 1 or 0 */
48         unsigned long curfreq; /* freq in kHz */
49         __u32 flags;
50 };
51
52 static int io = -1;
53 static int radio_nr = -1;
54 static struct pnp_dev *dev = NULL;
55 static struct mutex lock;
56
57 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
58 /* It is only useful to give freq in intervall of 800 (=0.05Mhz),
59  * other bits will be truncated, e.g 92.7400016 -> 92.7, but
60  * 92.7400017 -> 92.75
61  */
62 #define RSF16_ENCODE(x) ((x)/800+214)
63 #define RSF16_MINFREQ 87*16000
64 #define RSF16_MAXFREQ 108*16000
65
66 static void outbits(int bits, unsigned int data, int port)
67 {
68         while(bits--) {
69                 if(data & 1) {
70                         outb(5, port);
71                         udelay(6);
72                         outb(7, port);
73                         udelay(6);
74                 } else {
75                         outb(1, port);
76                         udelay(6);
77                         outb(3, port);
78                         udelay(6);
79                 }
80                 data>>=1;
81         }
82 }
83
84 static inline void fmi_mute(int port)
85 {
86         mutex_lock(&lock);
87         outb(0x00, port);
88         mutex_unlock(&lock);
89 }
90
91 static inline void fmi_unmute(int port)
92 {
93         mutex_lock(&lock);
94         outb(0x08, port);
95         mutex_unlock(&lock);
96 }
97
98 static inline int fmi_setfreq(struct fmi_device *dev)
99 {
100         int myport = dev->port;
101         unsigned long freq = dev->curfreq;
102
103         mutex_lock(&lock);
104
105         outbits(16, RSF16_ENCODE(freq), myport);
106         outbits(8, 0xC0, myport);
107         msleep(143);            /* was schedule_timeout(HZ/7) */
108         mutex_unlock(&lock);
109         if (dev->curvol) fmi_unmute(myport);
110         return 0;
111 }
112
113 static inline int fmi_getsigstr(struct fmi_device *dev)
114 {
115         int val;
116         int res;
117         int myport = dev->port;
118
119
120         mutex_lock(&lock);
121         val = dev->curvol ? 0x08 : 0x00;        /* unmute/mute */
122         outb(val, myport);
123         outb(val | 0x10, myport);
124         msleep(143);            /* was schedule_timeout(HZ/7) */
125         res = (int)inb(myport+1);
126         outb(val, myport);
127
128         mutex_unlock(&lock);
129         return (res & 2) ? 0 : 0xFFFF;
130 }
131
132 static int fmi_do_ioctl(struct inode *inode, struct file *file,
133                         unsigned int cmd, void *arg)
134 {
135         struct video_device *dev = video_devdata(file);
136         struct fmi_device *fmi=dev->priv;
137
138         switch(cmd)
139         {
140                 case VIDIOC_QUERYCAP:
141                 {
142                         struct v4l2_capability *v = arg;
143                         memset(v,0,sizeof(*v));
144                         strlcpy(v->driver, "radio-sf16fmi", sizeof (v->driver));
145                         strlcpy(v->card, "SF16-FMx radio", sizeof (v->card));
146                         sprintf(v->bus_info,"ISA");
147                         v->version = RADIO_VERSION;
148                         v->capabilities = V4L2_CAP_TUNER;
149
150                         return 0;
151                 }
152                 case VIDIOC_G_TUNER:
153                 {
154                         struct v4l2_tuner *v = arg;
155                         int mult;
156
157                         if (v->index > 0)
158                                 return -EINVAL;
159
160                         memset(v,0,sizeof(*v));
161                         strcpy(v->name, "FM");
162                         v->type = V4L2_TUNER_RADIO;
163
164                         mult = (fmi->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
165                         v->rangelow = RSF16_MINFREQ/mult;
166                         v->rangehigh = RSF16_MAXFREQ/mult;
167                         v->rxsubchans =V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
168                         v->capability=fmi->flags&V4L2_TUNER_CAP_LOW;
169                         v->audmode = V4L2_TUNER_MODE_STEREO;
170                         v->signal = fmi_getsigstr(fmi);
171
172                         return 0;
173                 }
174                 case VIDIOC_S_TUNER:
175                 {
176                         struct v4l2_tuner *v = arg;
177
178                         if (v->index > 0)
179                                 return -EINVAL;
180
181                         return 0;
182                 }
183                 case VIDIOC_S_FREQUENCY:
184                 {
185                         struct v4l2_frequency *f = arg;
186
187                         if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
188                                 f->frequency *= 1000;
189                         if (f->frequency < RSF16_MINFREQ ||
190                                         f->frequency > RSF16_MAXFREQ )
191                                 return -EINVAL;
192                         /*rounding in steps of 800 to match th freq
193                           that will be used */
194                         fmi->curfreq = (f->frequency/800)*800;
195                         fmi_setfreq(fmi);
196
197                         return 0;
198                 }
199                 case VIDIOC_G_FREQUENCY:
200                 {
201                         struct v4l2_frequency *f = arg;
202
203                         f->type = V4L2_TUNER_RADIO;
204                         f->frequency = fmi->curfreq;
205                         if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
206                                 f->frequency /= 1000;
207
208                         return 0;
209                 }
210                 case VIDIOC_QUERYCTRL:
211                 {
212                         struct v4l2_queryctrl *qc = arg;
213                         int i;
214
215                         for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
216                                 if (qc->id && qc->id == radio_qctrl[i].id) {
217                                         memcpy(qc, &(radio_qctrl[i]),
218                                                                 sizeof(*qc));
219                                         return (0);
220                                 }
221                         }
222                         return -EINVAL;
223                 }
224                 case VIDIOC_G_CTRL:
225                 {
226                         struct v4l2_control *ctrl= arg;
227
228                         switch (ctrl->id) {
229                                 case V4L2_CID_AUDIO_MUTE:
230                                         ctrl->value=fmi->curvol;
231                                         return (0);
232                         }
233                         return -EINVAL;
234                 }
235                 case VIDIOC_S_CTRL:
236                 {
237                         struct v4l2_control *ctrl= arg;
238
239                         switch (ctrl->id) {
240                                 case V4L2_CID_AUDIO_MUTE:
241                                 {
242                                         if (ctrl->value)
243                                                 fmi_mute(fmi->port);
244                                         else
245                                                 fmi_unmute(fmi->port);
246
247                                         fmi->curvol=ctrl->value;
248                                         return (0);
249                                 }
250                         }
251                         return -EINVAL;
252                 }
253                 default:
254                         return v4l_compat_translate_ioctl(inode,file,cmd,arg,
255                                                           fmi_do_ioctl);
256         }
257 }
258
259 static int fmi_ioctl(struct inode *inode, struct file *file,
260                      unsigned int cmd, unsigned long arg)
261 {
262         return video_usercopy(inode, file, cmd, arg, fmi_do_ioctl);
263 }
264
265 static struct fmi_device fmi_unit;
266
267 static struct file_operations fmi_fops = {
268         .owner          = THIS_MODULE,
269         .open           = video_exclusive_open,
270         .release        = video_exclusive_release,
271         .ioctl          = fmi_ioctl,
272         .compat_ioctl   = v4l_compat_ioctl32,
273         .llseek         = no_llseek,
274 };
275
276 static struct video_device fmi_radio=
277 {
278         .owner          = THIS_MODULE,
279         .name           = "SF16FMx radio",
280         .type           = VID_TYPE_TUNER,
281         .hardware       = 0,
282         .fops           = &fmi_fops,
283 };
284
285 /* ladis: this is my card. does any other types exist? */
286 static struct isapnp_device_id id_table[] __devinitdata = {
287         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
288                 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
289         {       ISAPNP_CARD_END, },
290 };
291
292 MODULE_DEVICE_TABLE(isapnp, id_table);
293
294 static int isapnp_fmi_probe(void)
295 {
296         int i = 0;
297
298         while (id_table[i].card_vendor != 0 && dev == NULL) {
299                 dev = pnp_find_dev(NULL, id_table[i].vendor,
300                                    id_table[i].function, NULL);
301                 i++;
302         }
303
304         if (!dev)
305                 return -ENODEV;
306         if (pnp_device_attach(dev) < 0)
307                 return -EAGAIN;
308         if (pnp_activate_dev(dev) < 0) {
309                 printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
310                 pnp_device_detach(dev);
311                 return -ENOMEM;
312         }
313         if (!pnp_port_valid(dev, 0)) {
314                 pnp_device_detach(dev);
315                 return -ENODEV;
316         }
317
318         i = pnp_port_start(dev, 0);
319         printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
320
321         return i;
322 }
323
324 static int __init fmi_init(void)
325 {
326         if (io < 0)
327                 io = isapnp_fmi_probe();
328         if (io < 0) {
329                 printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
330                 return io;
331         }
332         if (!request_region(io, 2, "radio-sf16fmi")) {
333                 printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
334                 return -EBUSY;
335         }
336
337         fmi_unit.port = io;
338         fmi_unit.curvol = 0;
339         fmi_unit.curfreq = 0;
340         fmi_unit.flags = V4L2_TUNER_CAP_LOW;
341         fmi_radio.priv = &fmi_unit;
342
343         mutex_init(&lock);
344
345         if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) == -1) {
346                 release_region(io, 2);
347                 return -EINVAL;
348         }
349
350         printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
351         /* mute card - prevents noisy bootups */
352         fmi_mute(io);
353         return 0;
354 }
355
356 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
357 MODULE_DESCRIPTION("A driver for the SF16MI radio.");
358 MODULE_LICENSE("GPL");
359
360 module_param(io, int, 0);
361 MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
362 module_param(radio_nr, int, 0);
363
364 static void __exit fmi_cleanup_module(void)
365 {
366         video_unregister_device(&fmi_radio);
367         release_region(io, 2);
368         if (dev)
369                 pnp_device_detach(dev);
370 }
371
372 module_init(fmi_init);
373 module_exit(fmi_cleanup_module);