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 <linux/videodev2.h> /* kernel radio structs */
37 #include <linux/mutex.h>
38 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
39 #include <linux/io.h> /* outb, outb_p */
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-ioctl.h>
43 MODULE_AUTHOR("C.van Schaik");
44 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
45 MODULE_LICENSE("GPL");
47 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
48 #define CONFIG_RADIO_ZOLTRIX_PORT -1
51 static int io
= CONFIG_RADIO_ZOLTRIX_PORT
;
52 static int radio_nr
= -1;
54 module_param(io
, int, 0);
55 MODULE_PARM_DESC(io
, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
56 module_param(radio_nr
, int, 0);
58 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
61 struct v4l2_device v4l2_dev
;
62 struct video_device vdev
;
65 unsigned long curfreq
;
71 static struct zoltrix zoltrix_card
;
73 static int zol_setvol(struct zoltrix
*zol
, int vol
)
79 mutex_lock(&zol
->lock
);
83 inb(zol
->io
+ 3); /* Zoltrix needs to be read to confirm */
84 mutex_unlock(&zol
->lock
);
88 outb(zol
->curvol
-1, zol
->io
);
91 mutex_unlock(&zol
->lock
);
95 static void zol_mute(struct zoltrix
*zol
)
98 mutex_lock(&zol
->lock
);
101 inb(zol
->io
+ 3); /* Zoltrix needs to be read to confirm */
102 mutex_unlock(&zol
->lock
);
105 static void zol_unmute(struct zoltrix
*zol
)
108 zol_setvol(zol
, zol
->curvol
);
111 static int zol_setfreq(struct zoltrix
*zol
, unsigned long freq
)
113 /* tunes the radio to the desired frequency */
114 struct v4l2_device
*v4l2_dev
= &zol
->v4l2_dev
;
115 unsigned long long bitmask
, f
, m
;
116 unsigned int stereo
= zol
->stereo
;
120 v4l2_warn(v4l2_dev
, "cannot set a frequency of 0.\n");
124 m
= (freq
/ 160 - 8800) * 2;
125 f
= (unsigned long long)m
+ 0x4d1c;
127 bitmask
= 0xc480402c10080000ull
;
130 mutex_lock(&zol
->lock
);
136 inb(zol
->io
+ 3); /* Zoltrix needs to be read to confirm */
141 bitmask
= (bitmask
^ ((f
& 0xff) << 47) ^ ((f
& 0xff00) << 30) ^ (stereo
<< 31));
143 if ((bitmask
& 0x8000000000000000ull
) != 0) {
160 /* termination sequence */
176 mutex_unlock(&zol
->lock
);
179 zol_setvol(zol
, zol
->curvol
);
183 /* Get signal strength */
184 static int zol_getsigstr(struct zoltrix
*zol
)
188 mutex_lock(&zol
->lock
);
189 outb(0x00, zol
->io
); /* This stuff I found to do nothing */
190 outb(zol
->curvol
, zol
->io
);
197 mutex_unlock(&zol
->lock
);
202 /* I found this out by playing with a binary scanner on the card io */
203 return a
== 0xcf || a
== 0xdf || a
== 0xef;
206 static int zol_is_stereo(struct zoltrix
*zol
)
210 mutex_lock(&zol
->lock
);
213 outb(zol
->curvol
, zol
->io
);
220 mutex_unlock(&zol
->lock
);
222 return x1
== x2
&& x1
== 0xcf;
225 static int vidioc_querycap(struct file
*file
, void *priv
,
226 struct v4l2_capability
*v
)
228 strlcpy(v
->driver
, "radio-zoltrix", sizeof(v
->driver
));
229 strlcpy(v
->card
, "Zoltrix Radio", sizeof(v
->card
));
230 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
231 v
->version
= RADIO_VERSION
;
232 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
236 static int vidioc_g_tuner(struct file
*file
, void *priv
,
237 struct v4l2_tuner
*v
)
239 struct zoltrix
*zol
= video_drvdata(file
);
244 strlcpy(v
->name
, "FM", sizeof(v
->name
));
245 v
->type
= V4L2_TUNER_RADIO
;
246 v
->rangelow
= 88 * 16000;
247 v
->rangehigh
= 108 * 16000;
248 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
249 v
->capability
= V4L2_TUNER_CAP_LOW
;
250 if (zol_is_stereo(zol
))
251 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
253 v
->audmode
= V4L2_TUNER_MODE_MONO
;
254 v
->signal
= 0xFFFF * zol_getsigstr(zol
);
258 static int vidioc_s_tuner(struct file
*file
, void *priv
,
259 struct v4l2_tuner
*v
)
261 return v
->index
? -EINVAL
: 0;
264 static int vidioc_s_frequency(struct file
*file
, void *priv
,
265 struct v4l2_frequency
*f
)
267 struct zoltrix
*zol
= video_drvdata(file
);
269 if (zol_setfreq(zol
, f
->frequency
) != 0)
274 static int vidioc_g_frequency(struct file
*file
, void *priv
,
275 struct v4l2_frequency
*f
)
277 struct zoltrix
*zol
= video_drvdata(file
);
279 f
->type
= V4L2_TUNER_RADIO
;
280 f
->frequency
= zol
->curfreq
;
284 static int vidioc_queryctrl(struct file
*file
, void *priv
,
285 struct v4l2_queryctrl
*qc
)
288 case V4L2_CID_AUDIO_MUTE
:
289 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
290 case V4L2_CID_AUDIO_VOLUME
:
291 return v4l2_ctrl_query_fill(qc
, 0, 65535, 4096, 65535);
296 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
297 struct v4l2_control
*ctrl
)
299 struct zoltrix
*zol
= video_drvdata(file
);
302 case V4L2_CID_AUDIO_MUTE
:
303 ctrl
->value
= zol
->muted
;
305 case V4L2_CID_AUDIO_VOLUME
:
306 ctrl
->value
= zol
->curvol
* 4096;
312 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
313 struct v4l2_control
*ctrl
)
315 struct zoltrix
*zol
= video_drvdata(file
);
318 case V4L2_CID_AUDIO_MUTE
:
323 zol_setvol(zol
, zol
->curvol
);
326 case V4L2_CID_AUDIO_VOLUME
:
327 zol_setvol(zol
, ctrl
->value
/ 4096);
331 if (zol_setfreq(zol
, zol
->curfreq
) != 0)
334 /* FIXME: Implement stereo/mono switch on V4L2 */
335 if (v
->mode
& VIDEO_SOUND_STEREO
) {
337 zol_setfreq(zol
, zol
->curfreq
);
339 if (v
->mode
& VIDEO_SOUND_MONO
) {
341 zol_setfreq(zol
, zol
->curfreq
);
347 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
353 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
355 return i
? -EINVAL
: 0;
358 static int vidioc_g_audio(struct file
*file
, void *priv
,
359 struct v4l2_audio
*a
)
362 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
363 a
->capability
= V4L2_AUDCAP_STEREO
;
367 static int vidioc_s_audio(struct file
*file
, void *priv
,
368 struct v4l2_audio
*a
)
370 return a
->index
? -EINVAL
: 0;
373 static const struct v4l2_file_operations zoltrix_fops
=
375 .owner
= THIS_MODULE
,
376 .ioctl
= video_ioctl2
,
379 static const struct v4l2_ioctl_ops zoltrix_ioctl_ops
= {
380 .vidioc_querycap
= vidioc_querycap
,
381 .vidioc_g_tuner
= vidioc_g_tuner
,
382 .vidioc_s_tuner
= vidioc_s_tuner
,
383 .vidioc_g_audio
= vidioc_g_audio
,
384 .vidioc_s_audio
= vidioc_s_audio
,
385 .vidioc_g_input
= vidioc_g_input
,
386 .vidioc_s_input
= vidioc_s_input
,
387 .vidioc_g_frequency
= vidioc_g_frequency
,
388 .vidioc_s_frequency
= vidioc_s_frequency
,
389 .vidioc_queryctrl
= vidioc_queryctrl
,
390 .vidioc_g_ctrl
= vidioc_g_ctrl
,
391 .vidioc_s_ctrl
= vidioc_s_ctrl
,
394 static int __init
zoltrix_init(void)
396 struct zoltrix
*zol
= &zoltrix_card
;
397 struct v4l2_device
*v4l2_dev
= &zol
->v4l2_dev
;
400 strlcpy(v4l2_dev
->name
, "zoltrix", sizeof(v4l2_dev
->name
));
403 v4l2_err(v4l2_dev
, "You must set an I/O address with io=0x20c or 0x30c\n");
406 if (zol
->io
!= 0x20c && zol
->io
!= 0x30c) {
407 v4l2_err(v4l2_dev
, "invalid port, try 0x20c or 0x30c\n");
411 if (!request_region(zol
->io
, 2, "zoltrix")) {
412 v4l2_err(v4l2_dev
, "port 0x%x already in use\n", zol
->io
);
416 res
= v4l2_device_register(NULL
, v4l2_dev
);
418 release_region(zol
->io
, 2);
419 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
423 strlcpy(zol
->vdev
.name
, v4l2_dev
->name
, sizeof(zol
->vdev
.name
));
424 zol
->vdev
.v4l2_dev
= v4l2_dev
;
425 zol
->vdev
.fops
= &zoltrix_fops
;
426 zol
->vdev
.ioctl_ops
= &zoltrix_ioctl_ops
;
427 zol
->vdev
.release
= video_device_release_empty
;
428 video_set_drvdata(&zol
->vdev
, zol
);
430 if (video_register_device(&zol
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
431 v4l2_device_unregister(v4l2_dev
);
432 release_region(zol
->io
, 2);
435 v4l2_info(v4l2_dev
, "Zoltrix Radio Plus card driver.\n");
437 mutex_init(&zol
->lock
);
439 /* mute card - prevents noisy bootups */
441 /* this ensures that the volume is all the way down */
454 static void __exit
zoltrix_exit(void)
456 struct zoltrix
*zol
= &zoltrix_card
;
458 video_unregister_device(&zol
->vdev
);
459 v4l2_device_unregister(&zol
->v4l2_dev
);
460 release_region(zol
->io
, 2);
463 module_init(zoltrix_init
);
464 module_exit(zoltrix_exit
);