1 /* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
5 * Due to the inconsistency in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
8 * It seems that the card is not linear to 0 volume. It cuts off
9 * at a low volume, and it is not possible (at least I have not found)
10 * to get fine volume control over the low volume range.
12 * Some code derived from code by Romolo Manfredini
15 * 1999-05-06 - (C. van Schaik)
16 * - Make signal strength and stereo scans
17 * kinder to cpu while in delay
18 * 1999-01-05 - (C. van Schaik)
19 * - Changed tuning to 1/160Mhz accuracy
20 * - Added stereo support
21 * (card defaults to stereo)
22 * (can explicitly force mono on the card)
23 * (can detect if station is in stereo)
24 * - Added unmute function
25 * - Reworked ioctl functions
26 * 2002-07-15 - Fix Stereo typo
28 * 2006-07-24 - Converted to V4L2 API
29 * by Mauro Carvalho Chehab <mchehab@infradead.org>
32 #include <linux/module.h> /* Modules */
33 #include <linux/init.h> /* Initdata */
34 #include <linux/ioport.h> /* request_region */
35 #include <linux/delay.h> /* udelay, msleep */
36 #include <asm/io.h> /* outb, outb_p */
37 #include <asm/uaccess.h> /* copy to/from user */
38 #include <linux/videodev2.h> /* kernel radio structs */
39 #include <media/v4l2-common.h>
40 #include <media/v4l2-ioctl.h>
42 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
43 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
45 static struct v4l2_queryctrl radio_qctrl
[] = {
47 .id
= V4L2_CID_AUDIO_MUTE
,
52 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
54 .id
= V4L2_CID_AUDIO_VOLUME
,
59 .default_value
= 0xff,
60 .type
= V4L2_CTRL_TYPE_INTEGER
,
64 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
65 #define CONFIG_RADIO_ZOLTRIX_PORT -1
68 static int io
= CONFIG_RADIO_ZOLTRIX_PORT
;
69 static int radio_nr
= -1;
74 unsigned long curfreq
;
80 static int zol_setvol(struct zol_device
*dev
, int vol
)
86 mutex_lock(&dev
->lock
);
90 inb(io
+ 3); /* Zoltrix needs to be read to confirm */
91 mutex_unlock(&dev
->lock
);
95 outb(dev
->curvol
-1, io
);
98 mutex_unlock(&dev
->lock
);
102 static void zol_mute(struct zol_device
*dev
)
105 mutex_lock(&dev
->lock
);
108 inb(io
+ 3); /* Zoltrix needs to be read to confirm */
109 mutex_unlock(&dev
->lock
);
112 static void zol_unmute(struct zol_device
*dev
)
115 zol_setvol(dev
, dev
->curvol
);
118 static int zol_setfreq(struct zol_device
*dev
, unsigned long freq
)
120 /* tunes the radio to the desired frequency */
121 unsigned long long bitmask
, f
, m
;
122 unsigned int stereo
= dev
->stereo
;
127 m
= (freq
/ 160 - 8800) * 2;
128 f
= (unsigned long long) m
+ 0x4d1c;
130 bitmask
= 0xc480402c10080000ull
;
133 mutex_lock(&dev
->lock
);
137 inb(io
+ 3); /* Zoltrix needs to be read to confirm */
142 bitmask
= (bitmask
^ ((f
& 0xff) << 47) ^ ((f
& 0xff00) << 30) ^ ( stereo
<< 31));
144 if ((bitmask
& 0x8000000000000000ull
) != 0) {
161 /* termination sequence */
178 mutex_unlock(&dev
->lock
);
182 zol_setvol(dev
, dev
->curvol
);
187 /* Get signal strength */
189 static int zol_getsigstr(struct zol_device
*dev
)
193 mutex_lock(&dev
->lock
);
194 outb(0x00, io
); /* This stuff I found to do nothing */
195 outb(dev
->curvol
, io
);
202 mutex_unlock(&dev
->lock
);
207 if ((a
== 0xcf) || (a
== 0xdf) /* I found this out by playing */
208 || (a
== 0xef)) /* with a binary scanner on the card io */
213 static int zol_is_stereo (struct zol_device
*dev
)
217 mutex_lock(&dev
->lock
);
220 outb(dev
->curvol
, io
);
227 mutex_unlock(&dev
->lock
);
229 if ((x1
== x2
) && (x1
== 0xcf))
234 static int vidioc_querycap(struct file
*file
, void *priv
,
235 struct v4l2_capability
*v
)
237 strlcpy(v
->driver
, "radio-zoltrix", sizeof(v
->driver
));
238 strlcpy(v
->card
, "Zoltrix Radio", sizeof(v
->card
));
239 sprintf(v
->bus_info
, "ISA");
240 v
->version
= RADIO_VERSION
;
241 v
->capabilities
= V4L2_CAP_TUNER
;
245 static int vidioc_g_tuner(struct file
*file
, void *priv
,
246 struct v4l2_tuner
*v
)
248 struct video_device
*dev
= video_devdata(file
);
249 struct zol_device
*zol
= dev
->priv
;
254 strcpy(v
->name
, "FM");
255 v
->type
= V4L2_TUNER_RADIO
;
256 v
->rangelow
= (88*16000);
257 v
->rangehigh
= (108*16000);
258 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
|V4L2_TUNER_SUB_STEREO
;
259 v
->capability
= V4L2_TUNER_CAP_LOW
;
260 if (zol_is_stereo(zol
))
261 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
263 v
->audmode
= V4L2_TUNER_MODE_MONO
;
264 v
->signal
= 0xFFFF*zol_getsigstr(zol
);
268 static int vidioc_s_tuner(struct file
*file
, void *priv
,
269 struct v4l2_tuner
*v
)
276 static int vidioc_s_frequency(struct file
*file
, void *priv
,
277 struct v4l2_frequency
*f
)
279 struct video_device
*dev
= video_devdata(file
);
280 struct zol_device
*zol
= dev
->priv
;
282 zol
->curfreq
= f
->frequency
;
283 zol_setfreq(zol
, zol
->curfreq
);
287 static int vidioc_g_frequency(struct file
*file
, void *priv
,
288 struct v4l2_frequency
*f
)
290 struct video_device
*dev
= video_devdata(file
);
291 struct zol_device
*zol
= dev
->priv
;
293 f
->type
= V4L2_TUNER_RADIO
;
294 f
->frequency
= zol
->curfreq
;
298 static int vidioc_queryctrl(struct file
*file
, void *priv
,
299 struct v4l2_queryctrl
*qc
)
303 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
304 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
305 memcpy(qc
, &(radio_qctrl
[i
]),
313 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
314 struct v4l2_control
*ctrl
)
316 struct video_device
*dev
= video_devdata(file
);
317 struct zol_device
*zol
= dev
->priv
;
320 case V4L2_CID_AUDIO_MUTE
:
321 ctrl
->value
= zol
->muted
;
323 case V4L2_CID_AUDIO_VOLUME
:
324 ctrl
->value
= zol
->curvol
* 4096;
330 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
331 struct v4l2_control
*ctrl
)
333 struct video_device
*dev
= video_devdata(file
);
334 struct zol_device
*zol
= dev
->priv
;
337 case V4L2_CID_AUDIO_MUTE
:
342 zol_setvol(zol
,zol
->curvol
);
345 case V4L2_CID_AUDIO_VOLUME
:
346 zol_setvol(zol
,ctrl
->value
/4096);
350 zol_setfreq(zol
, zol
->curfreq
);
352 /* FIXME: Implement stereo/mono switch on V4L2 */
353 if (v
->mode
& VIDEO_SOUND_STEREO
) {
355 zol_setfreq(zol
, zol
->curfreq
);
357 if (v
->mode
& VIDEO_SOUND_MONO
) {
359 zol_setfreq(zol
, zol
->curfreq
);
365 static int vidioc_g_audio(struct file
*file
, void *priv
,
366 struct v4l2_audio
*a
)
371 strcpy(a
->name
, "Radio");
372 a
->capability
= V4L2_AUDCAP_STEREO
;
376 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
382 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
389 static int vidioc_s_audio(struct file
*file
, void *priv
,
390 struct v4l2_audio
*a
)
397 static struct zol_device zoltrix_unit
;
399 static const struct file_operations zoltrix_fops
=
401 .owner
= THIS_MODULE
,
402 .open
= video_exclusive_open
,
403 .release
= video_exclusive_release
,
404 .ioctl
= video_ioctl2
,
406 .compat_ioctl
= v4l_compat_ioctl32
,
411 static const struct v4l2_ioctl_ops zoltrix_ioctl_ops
= {
412 .vidioc_querycap
= vidioc_querycap
,
413 .vidioc_g_tuner
= vidioc_g_tuner
,
414 .vidioc_s_tuner
= vidioc_s_tuner
,
415 .vidioc_g_audio
= vidioc_g_audio
,
416 .vidioc_s_audio
= vidioc_s_audio
,
417 .vidioc_g_input
= vidioc_g_input
,
418 .vidioc_s_input
= vidioc_s_input
,
419 .vidioc_g_frequency
= vidioc_g_frequency
,
420 .vidioc_s_frequency
= vidioc_s_frequency
,
421 .vidioc_queryctrl
= vidioc_queryctrl
,
422 .vidioc_g_ctrl
= vidioc_g_ctrl
,
423 .vidioc_s_ctrl
= vidioc_s_ctrl
,
426 static struct video_device zoltrix_radio
= {
427 .owner
= THIS_MODULE
,
428 .name
= "Zoltrix Radio Plus",
429 .type
= VID_TYPE_TUNER
,
430 .fops
= &zoltrix_fops
,
431 .ioctl_ops
= &zoltrix_ioctl_ops
,
434 static int __init
zoltrix_init(void)
437 printk(KERN_ERR
"You must set an I/O address with io=0x???\n");
440 if ((io
!= 0x20c) && (io
!= 0x30c)) {
441 printk(KERN_ERR
"zoltrix: invalid port, try 0x20c or 0x30c\n");
445 zoltrix_radio
.priv
= &zoltrix_unit
;
446 if (!request_region(io
, 2, "zoltrix")) {
447 printk(KERN_ERR
"zoltrix: port 0x%x already in use\n", io
);
451 if (video_register_device(&zoltrix_radio
, VFL_TYPE_RADIO
, radio_nr
) == -1)
453 release_region(io
, 2);
456 printk(KERN_INFO
"Zoltrix Radio Plus card driver.\n");
458 mutex_init(&zoltrix_unit
.lock
);
460 /* mute card - prevents noisy bootups */
462 /* this ensures that the volume is all the way down */
469 zoltrix_unit
.curvol
= 0;
470 zoltrix_unit
.stereo
= 1;
475 MODULE_AUTHOR("C.van Schaik");
476 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
477 MODULE_LICENSE("GPL");
479 module_param(io
, int, 0);
480 MODULE_PARM_DESC(io
, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
481 module_param(radio_nr
, int, 0);
483 static void __exit
zoltrix_cleanup_module(void)
485 video_unregister_device(&zoltrix_radio
);
486 release_region(io
, 2);
489 module_init(zoltrix_init
);
490 module_exit(zoltrix_cleanup_module
);