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