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 <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
41 MODULE_AUTHOR("M.Kirkwood");
42 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
43 MODULE_LICENSE("GPL");
45 #ifndef CONFIG_RADIO_RTRACK_PORT
46 #define CONFIG_RADIO_RTRACK_PORT -1
49 static int io
= CONFIG_RADIO_RTRACK_PORT
;
50 static int radio_nr
= -1;
52 module_param(io
, int, 0);
53 MODULE_PARM_DESC(io
, "I/O address of the RadioTrack card (0x20f or 0x30f)");
54 module_param(radio_nr
, int, 0);
56 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
60 struct v4l2_device v4l2_dev
;
61 struct video_device vdev
;
64 unsigned long curfreq
;
70 static struct rtrack rtrack_card
;
74 static void sleep_delay(long n
)
76 /* Sleep nicely for 'n' uS */
77 int d
= n
/ msecs_to_jiffies(1000);
81 msleep(jiffies_to_msecs(d
));
84 static void rt_decvol(struct rtrack
*rt
)
86 outb(0x58, rt
->io
); /* volume down + sigstr + on */
88 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
91 static void rt_incvol(struct rtrack
*rt
)
93 outb(0x98, rt
->io
); /* volume up + sigstr + on */
95 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
98 static void rt_mute(struct rtrack
*rt
)
101 mutex_lock(&rt
->lock
);
102 outb(0xd0, rt
->io
); /* volume steady, off */
103 mutex_unlock(&rt
->lock
);
106 static int rt_setvol(struct rtrack
*rt
, int vol
)
110 mutex_lock(&rt
->lock
);
112 if (vol
== rt
->curvol
) { /* requested volume = current */
113 if (rt
->muted
) { /* user is unmuting the card */
115 outb(0xd8, rt
->io
); /* enable card */
117 mutex_unlock(&rt
->lock
);
121 if (vol
== 0) { /* volume = 0 means mute the card */
122 outb(0x48, rt
->io
); /* volume down but still "on" */
123 sleep_delay(2000000); /* make sure it's totally down */
124 outb(0xd0, rt
->io
); /* volume steady, off */
125 rt
->curvol
= 0; /* track the volume state! */
126 mutex_unlock(&rt
->lock
);
131 if (vol
> rt
->curvol
)
132 for (i
= rt
->curvol
; i
< vol
; i
++)
135 for (i
= rt
->curvol
; i
> vol
; i
--)
139 mutex_unlock(&rt
->lock
);
143 /* the 128+64 on these outb's is to keep the volume stable while tuning
144 * without them, the volume _will_ creep up with each frequency change
145 * and bit 4 (+16) is to keep the signal strength meter enabled
148 static void send_0_byte(struct rtrack
*rt
)
150 if (rt
->curvol
== 0 || rt
->muted
) {
151 outb_p(128+64+16+ 1, rt
->io
); /* wr-enable + data low */
152 outb_p(128+64+16+2+1, rt
->io
); /* clock */
155 outb_p(128+64+16+8+ 1, rt
->io
); /* on + wr-enable + data low */
156 outb_p(128+64+16+8+2+1, rt
->io
); /* clock */
161 static void send_1_byte(struct rtrack
*rt
)
163 if (rt
->curvol
== 0 || rt
->muted
) {
164 outb_p(128+64+16+4 +1, rt
->io
); /* wr-enable+data high */
165 outb_p(128+64+16+4+2+1, rt
->io
); /* clock */
168 outb_p(128+64+16+8+4 +1, rt
->io
); /* on+wr-enable+data high */
169 outb_p(128+64+16+8+4+2+1, rt
->io
); /* clock */
175 static int rt_setfreq(struct rtrack
*rt
, unsigned long freq
)
179 mutex_lock(&rt
->lock
); /* Stop other ops interfering */
183 /* now uses VIDEO_TUNER_LOW for fine tuning */
185 freq
+= 171200; /* Add 10.7 MHz IF */
186 freq
/= 800; /* Convert to 50 kHz units */
188 send_0_byte(rt
); /* 0: LSB of frequency */
190 for (i
= 0; i
< 13; i
++) /* : frequency bits (1-13) */
196 send_0_byte(rt
); /* 14: test bit - always 0 */
197 send_0_byte(rt
); /* 15: test bit - always 0 */
199 send_0_byte(rt
); /* 16: band data 0 - always 0 */
200 send_0_byte(rt
); /* 17: band data 1 - always 0 */
201 send_0_byte(rt
); /* 18: band data 2 - always 0 */
202 send_0_byte(rt
); /* 19: time base - always 0 */
204 send_0_byte(rt
); /* 20: spacing (0 = 25 kHz) */
205 send_1_byte(rt
); /* 21: spacing (1 = 25 kHz) */
206 send_0_byte(rt
); /* 22: spacing (0 = 25 kHz) */
207 send_1_byte(rt
); /* 23: AM/FM (FM = 1, always) */
209 if (rt
->curvol
== 0 || rt
->muted
)
210 outb(0xd0, rt
->io
); /* volume steady + sigstr */
212 outb(0xd8, rt
->io
); /* volume steady + sigstr + on */
214 mutex_unlock(&rt
->lock
);
219 static int rt_getsigstr(struct rtrack
*rt
)
223 mutex_lock(&rt
->lock
);
224 if (inb(rt
->io
) & 2) /* bit set = no signal present */
226 mutex_unlock(&rt
->lock
);
230 static int vidioc_querycap(struct file
*file
, void *priv
,
231 struct v4l2_capability
*v
)
233 strlcpy(v
->driver
, "radio-aimslab", sizeof(v
->driver
));
234 strlcpy(v
->card
, "RadioTrack", sizeof(v
->card
));
235 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
236 v
->version
= RADIO_VERSION
;
237 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
241 static int vidioc_g_tuner(struct file
*file
, void *priv
,
242 struct v4l2_tuner
*v
)
244 struct rtrack
*rt
= video_drvdata(file
);
249 strlcpy(v
->name
, "FM", sizeof(v
->name
));
250 v
->type
= V4L2_TUNER_RADIO
;
251 v
->rangelow
= 87 * 16000;
252 v
->rangehigh
= 108 * 16000;
253 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
254 v
->capability
= V4L2_TUNER_CAP_LOW
;
255 v
->audmode
= V4L2_TUNER_MODE_MONO
;
256 v
->signal
= 0xffff * rt_getsigstr(rt
);
260 static int vidioc_s_tuner(struct file
*file
, void *priv
,
261 struct v4l2_tuner
*v
)
263 return v
->index
? -EINVAL
: 0;
266 static int vidioc_s_frequency(struct file
*file
, void *priv
,
267 struct v4l2_frequency
*f
)
269 struct rtrack
*rt
= video_drvdata(file
);
271 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
273 rt_setfreq(rt
, f
->frequency
);
277 static int vidioc_g_frequency(struct file
*file
, void *priv
,
278 struct v4l2_frequency
*f
)
280 struct rtrack
*rt
= video_drvdata(file
);
284 f
->type
= V4L2_TUNER_RADIO
;
285 f
->frequency
= rt
->curfreq
;
289 static int vidioc_queryctrl(struct file
*file
, void *priv
,
290 struct v4l2_queryctrl
*qc
)
293 case V4L2_CID_AUDIO_MUTE
:
294 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
295 case V4L2_CID_AUDIO_VOLUME
:
296 return v4l2_ctrl_query_fill(qc
, 0, 0xff, 1, 0xff);
301 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
302 struct v4l2_control
*ctrl
)
304 struct rtrack
*rt
= video_drvdata(file
);
307 case V4L2_CID_AUDIO_MUTE
:
308 ctrl
->value
= rt
->muted
;
310 case V4L2_CID_AUDIO_VOLUME
:
311 ctrl
->value
= rt
->curvol
;
317 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
318 struct v4l2_control
*ctrl
)
320 struct rtrack
*rt
= video_drvdata(file
);
323 case V4L2_CID_AUDIO_MUTE
:
327 rt_setvol(rt
, rt
->curvol
);
329 case V4L2_CID_AUDIO_VOLUME
:
330 rt_setvol(rt
, ctrl
->value
);
336 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
342 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
344 return i
? -EINVAL
: 0;
347 static int vidioc_g_audio(struct file
*file
, void *priv
,
348 struct v4l2_audio
*a
)
351 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
352 a
->capability
= V4L2_AUDCAP_STEREO
;
356 static int vidioc_s_audio(struct file
*file
, void *priv
,
357 struct v4l2_audio
*a
)
359 return a
->index
? -EINVAL
: 0;
362 static const struct v4l2_file_operations rtrack_fops
= {
363 .owner
= THIS_MODULE
,
364 .ioctl
= video_ioctl2
,
367 static const struct v4l2_ioctl_ops rtrack_ioctl_ops
= {
368 .vidioc_querycap
= vidioc_querycap
,
369 .vidioc_g_tuner
= vidioc_g_tuner
,
370 .vidioc_s_tuner
= vidioc_s_tuner
,
371 .vidioc_g_audio
= vidioc_g_audio
,
372 .vidioc_s_audio
= vidioc_s_audio
,
373 .vidioc_g_input
= vidioc_g_input
,
374 .vidioc_s_input
= vidioc_s_input
,
375 .vidioc_g_frequency
= vidioc_g_frequency
,
376 .vidioc_s_frequency
= vidioc_s_frequency
,
377 .vidioc_queryctrl
= vidioc_queryctrl
,
378 .vidioc_g_ctrl
= vidioc_g_ctrl
,
379 .vidioc_s_ctrl
= vidioc_s_ctrl
,
382 static int __init
rtrack_init(void)
384 struct rtrack
*rt
= &rtrack_card
;
385 struct v4l2_device
*v4l2_dev
= &rt
->v4l2_dev
;
388 strlcpy(v4l2_dev
->name
, "rtrack", sizeof(v4l2_dev
->name
));
392 v4l2_err(v4l2_dev
, "you must set an I/O address with io=0x20f or 0x30f\n");
396 if (!request_region(rt
->io
, 2, "rtrack")) {
397 v4l2_err(v4l2_dev
, "port 0x%x already in use\n", rt
->io
);
401 res
= v4l2_device_register(NULL
, v4l2_dev
);
403 release_region(rt
->io
, 2);
404 v4l2_err(v4l2_dev
, "could not register v4l2_device\n");
408 strlcpy(rt
->vdev
.name
, v4l2_dev
->name
, sizeof(rt
->vdev
.name
));
409 rt
->vdev
.v4l2_dev
= v4l2_dev
;
410 rt
->vdev
.fops
= &rtrack_fops
;
411 rt
->vdev
.ioctl_ops
= &rtrack_ioctl_ops
;
412 rt
->vdev
.release
= video_device_release_empty
;
413 video_set_drvdata(&rt
->vdev
, rt
);
415 if (video_register_device(&rt
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
416 v4l2_device_unregister(&rt
->v4l2_dev
);
417 release_region(rt
->io
, 2);
420 v4l2_info(v4l2_dev
, "AIMSlab RadioTrack/RadioReveal card driver.\n");
422 /* Set up the I/O locking */
424 mutex_init(&rt
->lock
);
426 /* mute card - prevents noisy bootups */
428 /* this ensures that the volume is all the way down */
429 outb(0x48, rt
->io
); /* volume down but still "on" */
430 sleep_delay(2000000); /* make sure it's totally down */
431 outb(0xc0, rt
->io
); /* steady volume, mute card */
436 static void __exit
rtrack_exit(void)
438 struct rtrack
*rt
= &rtrack_card
;
440 video_unregister_device(&rt
->vdev
);
441 v4l2_device_unregister(&rt
->v4l2_dev
);
442 release_region(rt
->io
, 2);
445 module_init(rtrack_init
);
446 module_exit(rtrack_exit
);