1 /* SF16-FMI and SF16-FMP radio driver for Linux radio support
2 * heavily based on rtrack driver...
4 * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
6 * Fitted to new interface by Alan Cox <alan@lxorguk.ukuu.org.uk>
7 * Made working and cleaned up functions <mikael.hedin@irf.se>
8 * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
10 * Notes on the hardware
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 SF16-FMI/SF16-FMP
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
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/isapnp.h>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h> /* kernel radio structs */
27 #include <linux/io.h> /* outb, outb_p */
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
31 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
32 MODULE_DESCRIPTION("A driver for the SF16-FMI and SF16-FMP radio.");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION("0.0.3");
37 static int radio_nr
= -1;
39 module_param(io
, int, 0);
40 MODULE_PARM_DESC(io
, "I/O address of the SF16-FMI or SF16-FMP card (0x284 or 0x384)");
41 module_param(radio_nr
, int, 0);
45 struct v4l2_device v4l2_dev
;
46 struct video_device vdev
;
49 unsigned long curfreq
; /* freq in kHz */
53 static struct fmi fmi_card
;
54 static struct pnp_dev
*dev
;
57 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
58 /* It is only useful to give freq in interval of 800 (=0.05Mhz),
59 * other bits will be truncated, e.g 92.7400016 -> 92.7, but
62 #define RSF16_ENCODE(x) ((x) / 800 + 214)
63 #define RSF16_MINFREQ (87 * 16000)
64 #define RSF16_MAXFREQ (108 * 16000)
66 static void outbits(int bits
, unsigned int data
, int io
)
84 static inline void fmi_mute(struct fmi
*fmi
)
86 mutex_lock(&fmi
->lock
);
88 mutex_unlock(&fmi
->lock
);
91 static inline void fmi_unmute(struct fmi
*fmi
)
93 mutex_lock(&fmi
->lock
);
95 mutex_unlock(&fmi
->lock
);
98 static inline int fmi_setfreq(struct fmi
*fmi
, unsigned long freq
)
100 mutex_lock(&fmi
->lock
);
103 outbits(16, RSF16_ENCODE(freq
), fmi
->io
);
104 outbits(8, 0xC0, fmi
->io
);
105 msleep(143); /* was schedule_timeout(HZ/7) */
106 mutex_unlock(&fmi
->lock
);
112 static inline int fmi_getsigstr(struct fmi
*fmi
)
117 mutex_lock(&fmi
->lock
);
118 val
= fmi
->mute
? 0x00 : 0x08; /* mute/unmute */
120 outb(val
| 0x10, fmi
->io
);
121 msleep(143); /* was schedule_timeout(HZ/7) */
122 res
= (int)inb(fmi
->io
+ 1);
125 mutex_unlock(&fmi
->lock
);
126 return (res
& 2) ? 0 : 0xFFFF;
129 static int vidioc_querycap(struct file
*file
, void *priv
,
130 struct v4l2_capability
*v
)
132 strlcpy(v
->driver
, "radio-sf16fmi", sizeof(v
->driver
));
133 strlcpy(v
->card
, "SF16-FMx radio", sizeof(v
->card
));
134 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
135 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
139 static int vidioc_g_tuner(struct file
*file
, void *priv
,
140 struct v4l2_tuner
*v
)
142 struct fmi
*fmi
= video_drvdata(file
);
147 strlcpy(v
->name
, "FM", sizeof(v
->name
));
148 v
->type
= V4L2_TUNER_RADIO
;
149 v
->rangelow
= RSF16_MINFREQ
;
150 v
->rangehigh
= RSF16_MAXFREQ
;
151 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
152 v
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_LOW
;
153 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
154 v
->signal
= fmi_getsigstr(fmi
);
158 static int vidioc_s_tuner(struct file
*file
, void *priv
,
159 struct v4l2_tuner
*v
)
161 return v
->index
? -EINVAL
: 0;
164 static int vidioc_s_frequency(struct file
*file
, void *priv
,
165 struct v4l2_frequency
*f
)
167 struct fmi
*fmi
= video_drvdata(file
);
169 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
171 if (f
->frequency
< RSF16_MINFREQ
||
172 f
->frequency
> RSF16_MAXFREQ
)
174 /* rounding in steps of 800 to match the freq
176 fmi_setfreq(fmi
, (f
->frequency
/ 800) * 800);
180 static int vidioc_g_frequency(struct file
*file
, void *priv
,
181 struct v4l2_frequency
*f
)
183 struct fmi
*fmi
= video_drvdata(file
);
187 f
->type
= V4L2_TUNER_RADIO
;
188 f
->frequency
= fmi
->curfreq
;
192 static int vidioc_queryctrl(struct file
*file
, void *priv
,
193 struct v4l2_queryctrl
*qc
)
196 case V4L2_CID_AUDIO_MUTE
:
197 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
202 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
203 struct v4l2_control
*ctrl
)
205 struct fmi
*fmi
= video_drvdata(file
);
208 case V4L2_CID_AUDIO_MUTE
:
209 ctrl
->value
= fmi
->mute
;
215 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
216 struct v4l2_control
*ctrl
)
218 struct fmi
*fmi
= video_drvdata(file
);
221 case V4L2_CID_AUDIO_MUTE
:
226 fmi
->mute
= ctrl
->value
;
232 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
238 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
240 return i
? -EINVAL
: 0;
243 static int vidioc_g_audio(struct file
*file
, void *priv
,
244 struct v4l2_audio
*a
)
247 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
248 a
->capability
= V4L2_AUDCAP_STEREO
;
252 static int vidioc_s_audio(struct file
*file
, void *priv
,
253 struct v4l2_audio
*a
)
255 return a
->index
? -EINVAL
: 0;
258 static const struct v4l2_file_operations fmi_fops
= {
259 .owner
= THIS_MODULE
,
260 .unlocked_ioctl
= video_ioctl2
,
263 static const struct v4l2_ioctl_ops fmi_ioctl_ops
= {
264 .vidioc_querycap
= vidioc_querycap
,
265 .vidioc_g_tuner
= vidioc_g_tuner
,
266 .vidioc_s_tuner
= vidioc_s_tuner
,
267 .vidioc_g_audio
= vidioc_g_audio
,
268 .vidioc_s_audio
= vidioc_s_audio
,
269 .vidioc_g_input
= vidioc_g_input
,
270 .vidioc_s_input
= vidioc_s_input
,
271 .vidioc_g_frequency
= vidioc_g_frequency
,
272 .vidioc_s_frequency
= vidioc_s_frequency
,
273 .vidioc_queryctrl
= vidioc_queryctrl
,
274 .vidioc_g_ctrl
= vidioc_g_ctrl
,
275 .vidioc_s_ctrl
= vidioc_s_ctrl
,
278 /* ladis: this is my card. does any other types exist? */
279 static struct isapnp_device_id id_table
[] __devinitdata
= {
280 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
281 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
282 { ISAPNP_CARD_END
, },
285 MODULE_DEVICE_TABLE(isapnp
, id_table
);
287 static int __init
isapnp_fmi_probe(void)
291 while (id_table
[i
].card_vendor
!= 0 && dev
== NULL
) {
292 dev
= pnp_find_dev(NULL
, id_table
[i
].vendor
,
293 id_table
[i
].function
, NULL
);
299 if (pnp_device_attach(dev
) < 0)
301 if (pnp_activate_dev(dev
) < 0) {
302 printk(KERN_ERR
"radio-sf16fmi: PnP configure failed (out of resources?)\n");
303 pnp_device_detach(dev
);
306 if (!pnp_port_valid(dev
, 0)) {
307 pnp_device_detach(dev
);
311 i
= pnp_port_start(dev
, 0);
312 printk(KERN_INFO
"radio-sf16fmi: PnP reports card at %#x\n", i
);
317 static int __init
fmi_init(void)
319 struct fmi
*fmi
= &fmi_card
;
320 struct v4l2_device
*v4l2_dev
= &fmi
->v4l2_dev
;
322 int probe_ports
[] = { 0, 0x284, 0x384 };
325 for (i
= 0; i
< ARRAY_SIZE(probe_ports
); i
++) {
328 io
= isapnp_fmi_probe();
333 if (!request_region(io
, 2, "radio-sf16fmi")) {
335 pnp_device_detach(dev
);
340 ((inb(io
) & 0xf9) == 0xf9 && (inb(io
) & 0x4) == 0))
342 release_region(io
, 2);
346 if (!request_region(io
, 2, "radio-sf16fmi")) {
347 printk(KERN_ERR
"radio-sf16fmi: port %#x already in use\n", io
);
350 if (inb(io
) == 0xff) {
351 printk(KERN_ERR
"radio-sf16fmi: card not present at %#x\n", io
);
352 release_region(io
, 2);
357 printk(KERN_ERR
"radio-sf16fmi: no cards found\n");
361 strlcpy(v4l2_dev
->name
, "sf16fmi", sizeof(v4l2_dev
->name
));
364 res
= v4l2_device_register(NULL
, v4l2_dev
);
366 release_region(fmi
->io
, 2);
368 pnp_device_detach(dev
);
369 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
373 strlcpy(fmi
->vdev
.name
, v4l2_dev
->name
, sizeof(fmi
->vdev
.name
));
374 fmi
->vdev
.v4l2_dev
= v4l2_dev
;
375 fmi
->vdev
.fops
= &fmi_fops
;
376 fmi
->vdev
.ioctl_ops
= &fmi_ioctl_ops
;
377 fmi
->vdev
.release
= video_device_release_empty
;
378 video_set_drvdata(&fmi
->vdev
, fmi
);
380 mutex_init(&fmi
->lock
);
382 /* mute card - prevents noisy bootups */
385 if (video_register_device(&fmi
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
386 v4l2_device_unregister(v4l2_dev
);
387 release_region(fmi
->io
, 2);
389 pnp_device_detach(dev
);
393 v4l2_info(v4l2_dev
, "card driver at 0x%x\n", fmi
->io
);
397 static void __exit
fmi_exit(void)
399 struct fmi
*fmi
= &fmi_card
;
401 video_unregister_device(&fmi
->vdev
);
402 v4l2_device_unregister(&fmi
->v4l2_dev
);
403 release_region(fmi
->io
, 2);
404 if (dev
&& pnp_attached
)
405 pnp_device_detach(dev
);
408 module_init(fmi_init
);
409 module_exit(fmi_exit
);