1 /* radiotrack (radioreveal) driver for Linux radio support
3 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
8 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
9 * Fine tuning/VIDEO_TUNER_LOW
10 * Frequency range expanded to start at 87 MHz
12 * TODO: Allow for more than one of these foolish entities :-)
14 * Notes on the hardware (reverse engineered from other peoples'
15 * reverse engineering of AIMS' code :-)
17 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
19 * The signal strength query is unsurprisingly inaccurate. And it seems
20 * to indicate that (on my card, at least) the frequency setting isn't
21 * too great. (I have to tune up .025MHz from what the freq should be
22 * to get a report that the thing is tuned.)
24 * Volume control is (ugh) analogue:
25 * out(port, start_increasing_volume);
27 * out(port, stop_changing_the_volume);
31 #include <linux/module.h> /* Modules */
32 #include <linux/init.h> /* Initdata */
33 #include <linux/ioport.h> /* request_region */
34 #include <linux/delay.h> /* udelay */
35 #include <linux/videodev2.h> /* kernel radio structs */
36 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
37 #include <linux/io.h> /* outb, outb_p */
38 #include <linux/uaccess.h> /* copy to/from user */
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
42 MODULE_AUTHOR("M.Kirkwood");
43 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
44 MODULE_LICENSE("GPL");
46 #ifndef CONFIG_RADIO_RTRACK_PORT
47 #define CONFIG_RADIO_RTRACK_PORT -1
50 static int io
= CONFIG_RADIO_RTRACK_PORT
;
51 static int radio_nr
= -1;
53 module_param(io
, int, 0);
54 MODULE_PARM_DESC(io
, "I/O address of the RadioTrack card (0x20f or 0x30f)");
55 module_param(radio_nr
, int, 0);
57 #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 rtrack rtrack_card
;
75 static void sleep_delay(long n
)
77 /* Sleep nicely for 'n' uS */
78 int d
= n
/ msecs_to_jiffies(1000);
82 msleep(jiffies_to_msecs(d
));
85 static void rt_decvol(struct rtrack
*rt
)
87 outb(0x58, rt
->io
); /* volume down + sigstr + on */
89 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
92 static void rt_incvol(struct rtrack
*rt
)
94 outb(0x98, rt
->io
); /* volume up + sigstr + on */
96 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
99 static void rt_mute(struct rtrack
*rt
)
102 mutex_lock(&rt
->lock
);
103 outb(0xd0, rt
->io
); /* volume steady, off */
104 mutex_unlock(&rt
->lock
);
107 static int rt_setvol(struct rtrack
*rt
, int vol
)
111 mutex_lock(&rt
->lock
);
113 if (vol
== rt
->curvol
) { /* requested volume = current */
114 if (rt
->muted
) { /* user is unmuting the card */
116 outb(0xd8, rt
->io
); /* enable card */
118 mutex_unlock(&rt
->lock
);
122 if (vol
== 0) { /* volume = 0 means mute the card */
123 outb(0x48, rt
->io
); /* volume down but still "on" */
124 sleep_delay(2000000); /* make sure it's totally down */
125 outb(0xd0, rt
->io
); /* volume steady, off */
126 rt
->curvol
= 0; /* track the volume state! */
127 mutex_unlock(&rt
->lock
);
132 if (vol
> rt
->curvol
)
133 for (i
= rt
->curvol
; i
< vol
; i
++)
136 for (i
= rt
->curvol
; i
> vol
; i
--)
140 mutex_unlock(&rt
->lock
);
144 /* the 128+64 on these outb's is to keep the volume stable while tuning
145 * without them, the volume _will_ creep up with each frequency change
146 * and bit 4 (+16) is to keep the signal strength meter enabled
149 static void send_0_byte(struct rtrack
*rt
)
151 if (rt
->curvol
== 0 || rt
->muted
) {
152 outb_p(128+64+16+ 1, rt
->io
); /* wr-enable + data low */
153 outb_p(128+64+16+2+1, rt
->io
); /* clock */
156 outb_p(128+64+16+8+ 1, rt
->io
); /* on + wr-enable + data low */
157 outb_p(128+64+16+8+2+1, rt
->io
); /* clock */
162 static void send_1_byte(struct rtrack
*rt
)
164 if (rt
->curvol
== 0 || rt
->muted
) {
165 outb_p(128+64+16+4 +1, rt
->io
); /* wr-enable+data high */
166 outb_p(128+64+16+4+2+1, rt
->io
); /* clock */
169 outb_p(128+64+16+8+4 +1, rt
->io
); /* on+wr-enable+data high */
170 outb_p(128+64+16+8+4+2+1, rt
->io
); /* clock */
176 static int rt_setfreq(struct rtrack
*rt
, unsigned long freq
)
180 mutex_lock(&rt
->lock
); /* Stop other ops interfering */
184 /* now uses VIDEO_TUNER_LOW for fine tuning */
186 freq
+= 171200; /* Add 10.7 MHz IF */
187 freq
/= 800; /* Convert to 50 kHz units */
189 send_0_byte(rt
); /* 0: LSB of frequency */
191 for (i
= 0; i
< 13; i
++) /* : frequency bits (1-13) */
197 send_0_byte(rt
); /* 14: test bit - always 0 */
198 send_0_byte(rt
); /* 15: test bit - always 0 */
200 send_0_byte(rt
); /* 16: band data 0 - always 0 */
201 send_0_byte(rt
); /* 17: band data 1 - always 0 */
202 send_0_byte(rt
); /* 18: band data 2 - always 0 */
203 send_0_byte(rt
); /* 19: time base - always 0 */
205 send_0_byte(rt
); /* 20: spacing (0 = 25 kHz) */
206 send_1_byte(rt
); /* 21: spacing (1 = 25 kHz) */
207 send_0_byte(rt
); /* 22: spacing (0 = 25 kHz) */
208 send_1_byte(rt
); /* 23: AM/FM (FM = 1, always) */
210 if (rt
->curvol
== 0 || rt
->muted
)
211 outb(0xd0, rt
->io
); /* volume steady + sigstr */
213 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
215 mutex_unlock(&rt
->lock
);
220 static int rt_getsigstr(struct rtrack
*rt
)
224 mutex_lock(&rt
->lock
);
225 if (inb(rt
->io
) & 2) /* bit set = no signal present */
227 mutex_unlock(&rt
->lock
);
231 static int vidioc_querycap(struct file
*file
, void *priv
,
232 struct v4l2_capability
*v
)
234 strlcpy(v
->driver
, "radio-aimslab", sizeof(v
->driver
));
235 strlcpy(v
->card
, "RadioTrack", sizeof(v
->card
));
236 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
237 v
->version
= RADIO_VERSION
;
238 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
242 static int vidioc_g_tuner(struct file
*file
, void *priv
,
243 struct v4l2_tuner
*v
)
245 struct rtrack
*rt
= video_drvdata(file
);
250 strlcpy(v
->name
, "FM", sizeof(v
->name
));
251 v
->type
= V4L2_TUNER_RADIO
;
252 v
->rangelow
= 87 * 16000;
253 v
->rangehigh
= 108 * 16000;
254 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
255 v
->capability
= V4L2_TUNER_CAP_LOW
;
256 v
->audmode
= V4L2_TUNER_MODE_MONO
;
257 v
->signal
= 0xffff * rt_getsigstr(rt
);
261 static int vidioc_s_tuner(struct file
*file
, void *priv
,
262 struct v4l2_tuner
*v
)
264 return v
->index
? -EINVAL
: 0;
267 static int vidioc_s_frequency(struct file
*file
, void *priv
,
268 struct v4l2_frequency
*f
)
270 struct rtrack
*rt
= video_drvdata(file
);
272 rt_setfreq(rt
, f
->frequency
);
276 static int vidioc_g_frequency(struct file
*file
, void *priv
,
277 struct v4l2_frequency
*f
)
279 struct rtrack
*rt
= video_drvdata(file
);
281 f
->type
= V4L2_TUNER_RADIO
;
282 f
->frequency
= rt
->curfreq
;
286 static int vidioc_queryctrl(struct file
*file
, void *priv
,
287 struct v4l2_queryctrl
*qc
)
290 case V4L2_CID_AUDIO_MUTE
:
291 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
292 case V4L2_CID_AUDIO_VOLUME
:
293 return v4l2_ctrl_query_fill(qc
, 0, 0xff, 1, 0xff);
298 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
299 struct v4l2_control
*ctrl
)
301 struct rtrack
*rt
= video_drvdata(file
);
304 case V4L2_CID_AUDIO_MUTE
:
305 ctrl
->value
= rt
->muted
;
307 case V4L2_CID_AUDIO_VOLUME
:
308 ctrl
->value
= rt
->curvol
;
314 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
315 struct v4l2_control
*ctrl
)
317 struct rtrack
*rt
= video_drvdata(file
);
320 case V4L2_CID_AUDIO_MUTE
:
324 rt_setvol(rt
, rt
->curvol
);
326 case V4L2_CID_AUDIO_VOLUME
:
327 rt_setvol(rt
, ctrl
->value
);
333 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
339 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
341 return i
? -EINVAL
: 0;
344 static int vidioc_g_audio(struct file
*file
, void *priv
,
345 struct v4l2_audio
*a
)
348 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
349 a
->capability
= V4L2_AUDCAP_STEREO
;
353 static int vidioc_s_audio(struct file
*file
, void *priv
,
354 struct v4l2_audio
*a
)
356 return a
->index
? -EINVAL
: 0;
359 static int rtrack_open(struct file
*file
)
364 static int rtrack_release(struct file
*file
)
369 static const struct v4l2_file_operations rtrack_fops
= {
370 .owner
= THIS_MODULE
,
372 .release
= rtrack_release
,
373 .ioctl
= video_ioctl2
,
376 static const struct v4l2_ioctl_ops rtrack_ioctl_ops
= {
377 .vidioc_querycap
= vidioc_querycap
,
378 .vidioc_g_tuner
= vidioc_g_tuner
,
379 .vidioc_s_tuner
= vidioc_s_tuner
,
380 .vidioc_g_audio
= vidioc_g_audio
,
381 .vidioc_s_audio
= vidioc_s_audio
,
382 .vidioc_g_input
= vidioc_g_input
,
383 .vidioc_s_input
= vidioc_s_input
,
384 .vidioc_g_frequency
= vidioc_g_frequency
,
385 .vidioc_s_frequency
= vidioc_s_frequency
,
386 .vidioc_queryctrl
= vidioc_queryctrl
,
387 .vidioc_g_ctrl
= vidioc_g_ctrl
,
388 .vidioc_s_ctrl
= vidioc_s_ctrl
,
391 static int __init
rtrack_init(void)
393 struct rtrack
*rt
= &rtrack_card
;
394 struct v4l2_device
*v4l2_dev
= &rt
->v4l2_dev
;
397 strlcpy(v4l2_dev
->name
, "rtrack", sizeof(v4l2_dev
->name
));
401 v4l2_err(v4l2_dev
, "you must set an I/O address with io=0x20f or 0x30f\n");
405 if (!request_region(rt
->io
, 2, "rtrack")) {
406 v4l2_err(v4l2_dev
, "port 0x%x already in use\n", rt
->io
);
410 res
= v4l2_device_register(NULL
, v4l2_dev
);
412 release_region(rt
->io
, 2);
413 v4l2_err(v4l2_dev
, "could not register v4l2_device\n");
417 strlcpy(rt
->vdev
.name
, v4l2_dev
->name
, sizeof(rt
->vdev
.name
));
418 rt
->vdev
.v4l2_dev
= v4l2_dev
;
419 rt
->vdev
.fops
= &rtrack_fops
;
420 rt
->vdev
.ioctl_ops
= &rtrack_ioctl_ops
;
421 rt
->vdev
.release
= video_device_release_empty
;
422 video_set_drvdata(&rt
->vdev
, rt
);
424 if (video_register_device(&rt
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
425 v4l2_device_unregister(&rt
->v4l2_dev
);
426 release_region(rt
->io
, 2);
429 v4l2_info(v4l2_dev
, "AIMSlab RadioTrack/RadioReveal card driver.\n");
431 /* Set up the I/O locking */
433 mutex_init(&rt
->lock
);
435 /* mute card - prevents noisy bootups */
437 /* this ensures that the volume is all the way down */
438 outb(0x48, rt
->io
); /* volume down but still "on" */
439 sleep_delay(2000000); /* make sure it's totally down */
440 outb(0xc0, rt
->io
); /* steady volume, mute card */
445 static void __exit
rtrack_exit(void)
447 struct rtrack
*rt
= &rtrack_card
;
449 video_unregister_device(&rt
->vdev
);
450 v4l2_device_unregister(&rt
->v4l2_dev
);
451 release_region(rt
->io
, 2);
454 module_init(rtrack_init
);
455 module_exit(rtrack_exit
);