1 /* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
5 * Notes on the hardware
7 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
8 * No volume control - only mute/unmute - you have to use line volume
10 * For read stereo/mono you must wait 0.1 sec after set frequency and
11 * card unmuted so I set frequency on unmute
12 * Signal handling seem to work only on autoscanning (not implemented)
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev2.h> /* kernel radio structs */
24 #include <media/v4l2-common.h>
25 #include <linux/mutex.h>
27 static struct mutex lock
;
29 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
30 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
32 static struct v4l2_queryctrl radio_qctrl
[] = {
34 .id
= V4L2_CID_AUDIO_MUTE
,
39 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
41 .id
= V4L2_CID_AUDIO_VOLUME
,
46 .default_value
= 0xff,
47 .type
= V4L2_CTRL_TYPE_INTEGER
,
55 # define debug_print(s) printk s
57 # define debug_print(s)
60 /* this should be static vars for module size */
64 int curvol
; /* 0-65535, if not volume 0 or 65535 */
66 int stereo
; /* card is producing stereo audio */
67 unsigned long curfreq
; /* freq in kHz */
72 static int io
= 0x384;
73 static int radio_nr
= -1;
75 /* hw precision is 12.5 kHz
76 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
77 * other bits will be truncated
79 #define RSF16_ENCODE(x) ((x)/200+856)
80 #define RSF16_MINFREQ 87*16000
81 #define RSF16_MAXFREQ 108*16000
83 static inline void wait(int n
,int port
)
85 for (;n
;--n
) inb(port
);
88 static void outbits(int bits
, unsigned int data
, int nWait
, int port
)
92 bit
= (data
>>bits
) & 1;
102 static inline void fmr2_mute(int port
)
108 static inline void fmr2_unmute(int port
)
114 static inline int fmr2_stereo_mode(int port
)
120 debug_print((KERN_DEBUG
"stereo: %d\n", n
));
124 static int fmr2_product_info(struct fmr2_device
*dev
)
126 int n
= inb(dev
->port
);
130 /* this should support volume set */
134 /* not volume (mine is 11) */
135 dev
->card_type
= (n
==128)?11:0;
139 static inline int fmr2_getsigstr(struct fmr2_device
*dev
)
141 /* !!! work only if scanning freq */
142 int port
= dev
->port
, res
= 0xffff;
145 if (!(inb(port
)&1)) res
= 0;
146 debug_print((KERN_DEBUG
"signal: %d\n", res
));
150 /* set frequency and unmute card */
151 static int fmr2_setfreq(struct fmr2_device
*dev
)
153 int port
= dev
->port
;
154 unsigned long freq
= dev
->curfreq
;
158 /* 0x42 for mono output
159 * 0x102 forward scanning
160 * 0x182 scansione avanti
162 outbits(9,0x2,3,port
);
163 outbits(16,RSF16_ENCODE(freq
),2,port
);
170 /* NOTE if mute this stop radio
171 you must set freq on unmute */
172 dev
->stereo
= fmr2_stereo_mode(port
);
176 /* !!! not tested, in my card this does't work !!! */
177 static int fmr2_setvolume(struct fmr2_device
*dev
)
179 int i
,a
,n
, port
= dev
->port
;
181 if (dev
->card_type
!= 11) return 1;
183 switch( (dev
->curvol
+(1<<11)) >> 12 )
185 case 0: case 1: n
= 0x21; break;
186 case 2: n
= 0x84; break;
187 case 3: n
= 0x90; break;
188 case 4: n
= 0x104; break;
189 case 5: n
= 0x110; break;
190 case 6: n
= 0x204; break;
191 case 7: n
= 0x210; break;
192 case 8: n
= 0x402; break;
193 case 9: n
= 0x404; break;
195 case 10: n
= 0x408; break;
196 case 11: n
= 0x410; break;
197 case 12: n
= 0x801; break;
198 case 13: n
= 0x802; break;
199 case 14: n
= 0x804; break;
200 case 15: n
= 0x808; break;
201 case 16: n
= 0x810; break;
205 a
= ((n
>> i
) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
215 a
= ((0x18 >> i
) & 1) << 6;
229 static int fmr2_do_ioctl(struct inode
*inode
, struct file
*file
,
230 unsigned int cmd
, void *arg
)
232 struct video_device
*dev
= video_devdata(file
);
233 struct fmr2_device
*fmr2
= dev
->priv
;
234 debug_print((KERN_DEBUG
"freq %ld flags %d vol %d mute %d "
235 "stereo %d type %d\n",
236 fmr2
->curfreq
, fmr2
->flags
, fmr2
->curvol
, fmr2
->mute
,
237 fmr2
->stereo
, fmr2
->card_type
));
241 case VIDIOC_QUERYCAP
:
243 struct v4l2_capability
*v
= arg
;
244 memset(v
,0,sizeof(*v
));
245 strlcpy(v
->driver
, "radio-sf16fmr2", sizeof (v
->driver
));
246 strlcpy(v
->card
, "SF16-FMR2 radio", sizeof (v
->card
));
247 sprintf(v
->bus_info
,"ISA");
248 v
->version
= RADIO_VERSION
;
249 v
->capabilities
= V4L2_CAP_TUNER
;
255 struct v4l2_tuner
*v
= arg
;
261 memset(v
,0,sizeof(*v
));
262 strcpy(v
->name
, "FM");
263 v
->type
= V4L2_TUNER_RADIO
;
265 mult
= (fmr2
->flags
& V4L2_TUNER_CAP_LOW
) ? 1 : 1000;
266 v
->rangelow
= RSF16_MINFREQ
/mult
;
267 v
->rangehigh
= RSF16_MAXFREQ
/mult
;
268 v
->rxsubchans
=V4L2_TUNER_SUB_MONO
| V4L2_TUNER_MODE_STEREO
;
269 v
->capability
=fmr2
->flags
&V4L2_TUNER_CAP_LOW
;
271 v
->audmode
= fmr2
->stereo
? V4L2_TUNER_MODE_STEREO
:
272 V4L2_TUNER_MODE_MONO
;
274 v
->signal
= fmr2_getsigstr(fmr2
);
281 struct v4l2_tuner
*v
= arg
;
288 case VIDIOC_S_FREQUENCY
:
290 struct v4l2_frequency
*f
= arg
;
292 if (!(fmr2
->flags
& V4L2_TUNER_CAP_LOW
))
293 f
->frequency
*= 1000;
294 if (f
->frequency
< RSF16_MINFREQ
||
295 f
->frequency
> RSF16_MAXFREQ
)
297 /*rounding in steps of 200 to match th freq
299 fmr2
->curfreq
= (f
->frequency
/200)*200;
301 /* set card freq (if not muted) */
302 if (fmr2
->curvol
&& !fmr2
->mute
)
311 case VIDIOC_G_FREQUENCY
:
313 struct v4l2_frequency
*f
= arg
;
315 f
->type
= V4L2_TUNER_RADIO
;
316 f
->frequency
= fmr2
->curfreq
;
317 if (!(fmr2
->flags
& V4L2_TUNER_CAP_LOW
))
318 f
->frequency
/= 1000;
322 case VIDIOC_QUERYCTRL
:
324 struct v4l2_queryctrl
*qc
= arg
;
327 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
328 if ((fmr2
->card_type
!= 11)
329 && V4L2_CID_AUDIO_VOLUME
)
330 radio_qctrl
[i
].step
=65535;
331 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
332 memcpy(qc
, &(radio_qctrl
[i
]),
341 struct v4l2_control
*ctrl
= arg
;
344 case V4L2_CID_AUDIO_MUTE
:
345 ctrl
->value
=fmr2
->mute
;
347 case V4L2_CID_AUDIO_VOLUME
:
348 ctrl
->value
=fmr2
->curvol
;
355 struct v4l2_control
*ctrl
= arg
;
358 case V4L2_CID_AUDIO_MUTE
:
359 fmr2
->mute
=ctrl
->value
;
360 if (fmr2
->card_type
!= 11) {
362 fmr2
->curvol
= 65535;
368 case V4L2_CID_AUDIO_VOLUME
:
369 fmr2
->curvol
= ctrl
->value
;
370 if (fmr2
->card_type
!= 11) {
372 fmr2
->curvol
= 65535;
384 if (fmr2
->curvol
&& !fmr2
->mute
)
385 printk(KERN_DEBUG
"unmute\n");
387 printk(KERN_DEBUG
"mute\n");
390 if (fmr2
->curvol
&& !fmr2
->mute
) {
391 fmr2_setvolume(fmr2
);
394 fmr2_mute(fmr2
->port
);
399 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
405 static int fmr2_ioctl(struct inode
*inode
, struct file
*file
,
406 unsigned int cmd
, unsigned long arg
)
408 return video_usercopy(inode
, file
, cmd
, arg
, fmr2_do_ioctl
);
411 static struct fmr2_device fmr2_unit
;
413 static const struct file_operations fmr2_fops
= {
414 .owner
= THIS_MODULE
,
415 .open
= video_exclusive_open
,
416 .release
= video_exclusive_release
,
418 .compat_ioctl
= v4l_compat_ioctl32
,
422 static struct video_device fmr2_radio
=
424 .owner
= THIS_MODULE
,
425 .name
= "SF16FMR2 radio",
426 . type
= VID_TYPE_TUNER
,
431 static int __init
fmr2_init(void)
434 fmr2_unit
.curvol
= 0;
436 fmr2_unit
.curfreq
= 0;
437 fmr2_unit
.stereo
= 1;
438 fmr2_unit
.flags
= V4L2_TUNER_CAP_LOW
;
439 fmr2_unit
.card_type
= 0;
440 fmr2_radio
.priv
= &fmr2_unit
;
444 if (request_region(io
, 2, "sf16fmr2"))
446 printk(KERN_ERR
"fmr2: port 0x%x already in use\n", io
);
450 if(video_register_device(&fmr2_radio
, VFL_TYPE_RADIO
, radio_nr
)==-1)
452 release_region(io
, 2);
456 printk(KERN_INFO
"SF16FMR2 radio card driver at 0x%x.\n", io
);
457 /* mute card - prevents noisy bootups */
460 fmr2_product_info(&fmr2_unit
);
462 debug_print((KERN_DEBUG
"card_type %d\n", fmr2_unit
.card_type
));
466 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
467 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
468 MODULE_LICENSE("GPL");
470 module_param(io
, int, 0);
471 MODULE_PARM_DESC(io
, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
472 module_param(radio_nr
, int, 0);
474 static void __exit
fmr2_cleanup_module(void)
476 video_unregister_device(&fmr2_radio
);
477 release_region(io
,2);
480 module_init(fmr2_init
);
481 module_exit(fmr2_cleanup_module
);
485 static int __init
fmr2_setup_io(char *str
)
487 get_option(&str
, &io
);
491 __setup("sf16fmr2=", fmr2_setup_io
);