1 /* SF16-FMI, SF16-FMP and SF16-FMD 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/SF16-FMD
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>
32 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
33 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION("0.0.3");
38 static int radio_nr
= -1;
40 module_param(io
, int, 0);
41 MODULE_PARM_DESC(io
, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
42 module_param(radio_nr
, int, 0);
46 struct v4l2_device v4l2_dev
;
47 struct video_device vdev
;
50 unsigned long curfreq
; /* freq in kHz */
54 static struct fmi fmi_card
;
55 static struct pnp_dev
*dev
;
58 #define RSF16_MINFREQ (87 * 16000)
59 #define RSF16_MAXFREQ (108 * 16000)
61 #define FMI_BIT_TUN_CE (1 << 0)
62 #define FMI_BIT_TUN_CLK (1 << 1)
63 #define FMI_BIT_TUN_DATA (1 << 2)
64 #define FMI_BIT_VOL_SW (1 << 3)
65 #define FMI_BIT_TUN_STRQ (1 << 4)
67 static void fmi_set_pins(void *handle
, u8 pins
)
69 struct fmi
*fmi
= handle
;
70 u8 bits
= FMI_BIT_TUN_STRQ
;
73 bits
|= FMI_BIT_VOL_SW
;
75 if (pins
& LM7000_DATA
)
76 bits
|= FMI_BIT_TUN_DATA
;
77 if (pins
& LM7000_CLK
)
78 bits
|= FMI_BIT_TUN_CLK
;
80 bits
|= FMI_BIT_TUN_CE
;
82 mutex_lock(&fmi
->lock
);
83 outb_p(bits
, fmi
->io
);
84 mutex_unlock(&fmi
->lock
);
87 static inline void fmi_mute(struct fmi
*fmi
)
89 mutex_lock(&fmi
->lock
);
91 mutex_unlock(&fmi
->lock
);
94 static inline void fmi_unmute(struct fmi
*fmi
)
96 mutex_lock(&fmi
->lock
);
98 mutex_unlock(&fmi
->lock
);
101 static inline int fmi_getsigstr(struct fmi
*fmi
)
106 mutex_lock(&fmi
->lock
);
107 val
= fmi
->mute
? 0x00 : 0x08; /* mute/unmute */
109 outb(val
| 0x10, fmi
->io
);
110 msleep(143); /* was schedule_timeout(HZ/7) */
111 res
= (int)inb(fmi
->io
+ 1);
114 mutex_unlock(&fmi
->lock
);
115 return (res
& 2) ? 0 : 0xFFFF;
118 static int vidioc_querycap(struct file
*file
, void *priv
,
119 struct v4l2_capability
*v
)
121 strlcpy(v
->driver
, "radio-sf16fmi", sizeof(v
->driver
));
122 strlcpy(v
->card
, "SF16-FMI/FMP/FMD radio", sizeof(v
->card
));
123 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
124 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
128 static int vidioc_g_tuner(struct file
*file
, void *priv
,
129 struct v4l2_tuner
*v
)
131 struct fmi
*fmi
= video_drvdata(file
);
136 strlcpy(v
->name
, "FM", sizeof(v
->name
));
137 v
->type
= V4L2_TUNER_RADIO
;
138 v
->rangelow
= RSF16_MINFREQ
;
139 v
->rangehigh
= RSF16_MAXFREQ
;
140 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
141 v
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_LOW
;
142 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
143 v
->signal
= fmi_getsigstr(fmi
);
147 static int vidioc_s_tuner(struct file
*file
, void *priv
,
148 const struct v4l2_tuner
*v
)
150 return v
->index
? -EINVAL
: 0;
153 static int vidioc_s_frequency(struct file
*file
, void *priv
,
154 const struct v4l2_frequency
*f
)
156 struct fmi
*fmi
= video_drvdata(file
);
158 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
160 if (f
->frequency
< RSF16_MINFREQ
||
161 f
->frequency
> RSF16_MAXFREQ
)
163 /* rounding in steps of 800 to match the freq
165 lm7000_set_freq((f
->frequency
/ 800) * 800, fmi
, fmi_set_pins
);
169 static int vidioc_g_frequency(struct file
*file
, void *priv
,
170 struct v4l2_frequency
*f
)
172 struct fmi
*fmi
= video_drvdata(file
);
176 f
->type
= V4L2_TUNER_RADIO
;
177 f
->frequency
= fmi
->curfreq
;
181 static int vidioc_queryctrl(struct file
*file
, void *priv
,
182 struct v4l2_queryctrl
*qc
)
185 case V4L2_CID_AUDIO_MUTE
:
186 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
191 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
192 struct v4l2_control
*ctrl
)
194 struct fmi
*fmi
= video_drvdata(file
);
197 case V4L2_CID_AUDIO_MUTE
:
198 ctrl
->value
= fmi
->mute
;
204 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
205 struct v4l2_control
*ctrl
)
207 struct fmi
*fmi
= video_drvdata(file
);
210 case V4L2_CID_AUDIO_MUTE
:
215 fmi
->mute
= ctrl
->value
;
221 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
227 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
229 return i
? -EINVAL
: 0;
232 static int vidioc_g_audio(struct file
*file
, void *priv
,
233 struct v4l2_audio
*a
)
236 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
237 a
->capability
= V4L2_AUDCAP_STEREO
;
241 static int vidioc_s_audio(struct file
*file
, void *priv
,
242 const struct v4l2_audio
*a
)
244 return a
->index
? -EINVAL
: 0;
247 static const struct v4l2_file_operations fmi_fops
= {
248 .owner
= THIS_MODULE
,
249 .unlocked_ioctl
= video_ioctl2
,
252 static const struct v4l2_ioctl_ops fmi_ioctl_ops
= {
253 .vidioc_querycap
= vidioc_querycap
,
254 .vidioc_g_tuner
= vidioc_g_tuner
,
255 .vidioc_s_tuner
= vidioc_s_tuner
,
256 .vidioc_g_audio
= vidioc_g_audio
,
257 .vidioc_s_audio
= vidioc_s_audio
,
258 .vidioc_g_input
= vidioc_g_input
,
259 .vidioc_s_input
= vidioc_s_input
,
260 .vidioc_g_frequency
= vidioc_g_frequency
,
261 .vidioc_s_frequency
= vidioc_s_frequency
,
262 .vidioc_queryctrl
= vidioc_queryctrl
,
263 .vidioc_g_ctrl
= vidioc_g_ctrl
,
264 .vidioc_s_ctrl
= vidioc_s_ctrl
,
267 /* ladis: this is my card. does any other types exist? */
268 static struct isapnp_device_id id_table
[] = {
270 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
271 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
273 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
274 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
275 { ISAPNP_CARD_END
, },
278 MODULE_DEVICE_TABLE(isapnp
, id_table
);
280 static int __init
isapnp_fmi_probe(void)
284 while (id_table
[i
].card_vendor
!= 0 && dev
== NULL
) {
285 dev
= pnp_find_dev(NULL
, id_table
[i
].vendor
,
286 id_table
[i
].function
, NULL
);
292 if (pnp_device_attach(dev
) < 0)
294 if (pnp_activate_dev(dev
) < 0) {
295 printk(KERN_ERR
"radio-sf16fmi: PnP configure failed (out of resources?)\n");
296 pnp_device_detach(dev
);
299 if (!pnp_port_valid(dev
, 0)) {
300 pnp_device_detach(dev
);
304 i
= pnp_port_start(dev
, 0);
305 printk(KERN_INFO
"radio-sf16fmi: PnP reports card at %#x\n", i
);
310 static int __init
fmi_init(void)
312 struct fmi
*fmi
= &fmi_card
;
313 struct v4l2_device
*v4l2_dev
= &fmi
->v4l2_dev
;
315 int probe_ports
[] = { 0, 0x284, 0x384 };
318 for (i
= 0; i
< ARRAY_SIZE(probe_ports
); i
++) {
321 io
= isapnp_fmi_probe();
326 if (!request_region(io
, 2, "radio-sf16fmi")) {
328 pnp_device_detach(dev
);
333 ((inb(io
) & 0xf9) == 0xf9 && (inb(io
) & 0x4) == 0))
335 release_region(io
, 2);
339 if (!request_region(io
, 2, "radio-sf16fmi")) {
340 printk(KERN_ERR
"radio-sf16fmi: port %#x already in use\n", io
);
343 if (inb(io
) == 0xff) {
344 printk(KERN_ERR
"radio-sf16fmi: card not present at %#x\n", io
);
345 release_region(io
, 2);
350 printk(KERN_ERR
"radio-sf16fmi: no cards found\n");
354 strlcpy(v4l2_dev
->name
, "sf16fmi", sizeof(v4l2_dev
->name
));
357 res
= v4l2_device_register(NULL
, v4l2_dev
);
359 release_region(fmi
->io
, 2);
361 pnp_device_detach(dev
);
362 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
366 strlcpy(fmi
->vdev
.name
, v4l2_dev
->name
, sizeof(fmi
->vdev
.name
));
367 fmi
->vdev
.v4l2_dev
= v4l2_dev
;
368 fmi
->vdev
.fops
= &fmi_fops
;
369 fmi
->vdev
.ioctl_ops
= &fmi_ioctl_ops
;
370 fmi
->vdev
.release
= video_device_release_empty
;
371 video_set_drvdata(&fmi
->vdev
, fmi
);
373 mutex_init(&fmi
->lock
);
375 /* mute card - prevents noisy bootups */
378 if (video_register_device(&fmi
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
379 v4l2_device_unregister(v4l2_dev
);
380 release_region(fmi
->io
, 2);
382 pnp_device_detach(dev
);
386 v4l2_info(v4l2_dev
, "card driver at 0x%x\n", fmi
->io
);
390 static void __exit
fmi_exit(void)
392 struct fmi
*fmi
= &fmi_card
;
394 video_unregister_device(&fmi
->vdev
);
395 v4l2_device_unregister(&fmi
->v4l2_dev
);
396 release_region(fmi
->io
, 2);
397 if (dev
&& pnp_attached
)
398 pnp_device_detach(dev
);
401 module_init(fmi_init
);
402 module_exit(fmi_exit
);