1 /* SF16FMI 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.cox@linux.org>
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 SF16FMI
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/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>
31 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
33 static struct v4l2_queryctrl radio_qctrl
[] = {
35 .id
= V4L2_CID_AUDIO_MUTE
,
40 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
47 int curvol
; /* 1 or 0 */
48 unsigned long curfreq
; /* freq in kHz */
53 static int radio_nr
= -1;
54 static struct pnp_dev
*dev
= NULL
;
55 static struct mutex lock
;
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
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 port
)
84 static inline void fmi_mute(int port
)
91 static inline void fmi_unmute(int port
)
98 static inline int fmi_setfreq(struct fmi_device
*dev
)
100 int myport
= dev
->port
;
101 unsigned long freq
= dev
->curfreq
;
105 outbits(16, RSF16_ENCODE(freq
), myport
);
106 outbits(8, 0xC0, myport
);
107 msleep(143); /* was schedule_timeout(HZ/7) */
109 if (dev
->curvol
) fmi_unmute(myport
);
113 static inline int fmi_getsigstr(struct fmi_device
*dev
)
117 int myport
= dev
->port
;
121 val
= dev
->curvol
? 0x08 : 0x00; /* unmute/mute */
123 outb(val
| 0x10, myport
);
124 msleep(143); /* was schedule_timeout(HZ/7) */
125 res
= (int)inb(myport
+1);
129 return (res
& 2) ? 0 : 0xFFFF;
132 static int fmi_do_ioctl(struct inode
*inode
, struct file
*file
,
133 unsigned int cmd
, void *arg
)
135 struct video_device
*dev
= video_devdata(file
);
136 struct fmi_device
*fmi
=dev
->priv
;
140 case VIDIOC_QUERYCAP
:
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
;
154 struct v4l2_tuner
*v
= arg
;
160 memset(v
,0,sizeof(*v
));
161 strcpy(v
->name
, "FM");
162 v
->type
= V4L2_TUNER_RADIO
;
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
);
176 struct v4l2_tuner
*v
= arg
;
183 case VIDIOC_S_FREQUENCY
:
185 struct v4l2_frequency
*f
= arg
;
187 if (!(fmi
->flags
& V4L2_TUNER_CAP_LOW
))
188 f
->frequency
*= 1000;
189 if (f
->frequency
< RSF16_MINFREQ
||
190 f
->frequency
> RSF16_MAXFREQ
)
192 /*rounding in steps of 800 to match th freq
194 fmi
->curfreq
= (f
->frequency
/800)*800;
199 case VIDIOC_G_FREQUENCY
:
201 struct v4l2_frequency
*f
= arg
;
203 f
->type
= V4L2_TUNER_RADIO
;
204 f
->frequency
= fmi
->curfreq
;
205 if (!(fmi
->flags
& V4L2_TUNER_CAP_LOW
))
206 f
->frequency
/= 1000;
210 case VIDIOC_QUERYCTRL
:
212 struct v4l2_queryctrl
*qc
= arg
;
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
]),
226 struct v4l2_control
*ctrl
= arg
;
229 case V4L2_CID_AUDIO_MUTE
:
230 ctrl
->value
=fmi
->curvol
;
237 struct v4l2_control
*ctrl
= arg
;
240 case V4L2_CID_AUDIO_MUTE
:
245 fmi_unmute(fmi
->port
);
247 fmi
->curvol
=ctrl
->value
;
254 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
259 static int fmi_ioctl(struct inode
*inode
, struct file
*file
,
260 unsigned int cmd
, unsigned long arg
)
262 return video_usercopy(inode
, file
, cmd
, arg
, fmi_do_ioctl
);
265 static struct fmi_device fmi_unit
;
267 static struct file_operations fmi_fops
= {
268 .owner
= THIS_MODULE
,
269 .open
= video_exclusive_open
,
270 .release
= video_exclusive_release
,
272 .compat_ioctl
= v4l_compat_ioctl32
,
276 static struct video_device fmi_radio
=
278 .owner
= THIS_MODULE
,
279 .name
= "SF16FMx radio",
280 .type
= VID_TYPE_TUNER
,
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
, },
292 MODULE_DEVICE_TABLE(isapnp
, id_table
);
294 static int isapnp_fmi_probe(void)
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
);
306 if (pnp_device_attach(dev
) < 0)
308 if (pnp_activate_dev(dev
) < 0) {
309 printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
310 pnp_device_detach(dev
);
313 if (!pnp_port_valid(dev
, 0)) {
314 pnp_device_detach(dev
);
318 i
= pnp_port_start(dev
, 0);
319 printk ("radio-sf16fmi: PnP reports card at %#x\n", i
);
324 static int __init
fmi_init(void)
327 io
= isapnp_fmi_probe();
329 printk(KERN_ERR
"radio-sf16fmi: No PnP card found.\n");
332 if (!request_region(io
, 2, "radio-sf16fmi")) {
333 printk(KERN_ERR
"radio-sf16fmi: port 0x%x already in use\n", io
);
339 fmi_unit
.curfreq
= 0;
340 fmi_unit
.flags
= V4L2_TUNER_CAP_LOW
;
341 fmi_radio
.priv
= &fmi_unit
;
345 if (video_register_device(&fmi_radio
, VFL_TYPE_RADIO
, radio_nr
) == -1) {
346 release_region(io
, 2);
350 printk(KERN_INFO
"SF16FMx radio card driver at 0x%x\n", io
);
351 /* mute card - prevents noisy bootups */
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");
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);
364 static void __exit
fmi_cleanup_module(void)
366 video_unregister_device(&fmi_radio
);
367 release_region(io
, 2);
369 pnp_device_detach(dev
);
372 module_init(fmi_init
);
373 module_exit(fmi_cleanup_module
);