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 <asm/io.h> /* outb, outb_p */
36 #include <asm/uaccess.h> /* copy to/from user */
37 #include <linux/videodev2.h> /* kernel radio structs */
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
41 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
42 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
44 #ifndef CONFIG_RADIO_RTRACK_PORT
45 #define CONFIG_RADIO_RTRACK_PORT -1
48 static int io = CONFIG_RADIO_RTRACK_PORT;
49 static int radio_nr = -1;
50 static struct mutex lock;
57 unsigned long curfreq;
64 static void sleep_delay(long n)
66 /* Sleep nicely for 'n' uS */
67 int d=n/msecs_to_jiffies(1000);
71 msleep(jiffies_to_msecs(d));
74 static void rt_decvol(void)
76 outb(0x58, io); /* volume down + sigstr + on */
78 outb(0xd8, io); /* volume steady + sigstr + on */
81 static void rt_incvol(void)
83 outb(0x98, io); /* volume up + sigstr + on */
85 outb(0xd8, io); /* volume steady + sigstr + on */
88 static void rt_mute(struct rt_device *dev)
92 outb(0xd0, io); /* volume steady, off */
96 static int rt_setvol(struct rt_device *dev, int vol)
102 if(vol == dev->curvol) { /* requested volume = current */
103 if (dev->muted) { /* user is unmuting the card */
105 outb (0xd8, io); /* enable card */
111 if(vol == 0) { /* volume = 0 means mute the card */
112 outb(0x48, io); /* volume down but still "on" */
113 sleep_delay(2000000); /* make sure it's totally down */
114 outb(0xd0, io); /* volume steady, off */
115 dev->curvol = 0; /* track the volume state! */
121 if(vol > dev->curvol)
122 for(i = dev->curvol; i < vol; i++)
125 for(i = dev->curvol; i > vol; i--)
133 /* the 128+64 on these outb's is to keep the volume stable while tuning
134 * without them, the volume _will_ creep up with each frequency change
135 * and bit 4 (+16) is to keep the signal strength meter enabled
138 static void send_0_byte(int port, struct rt_device *dev)
140 if ((dev->curvol == 0) || (dev->muted)) {
141 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
142 outb_p(128+64+16+2+1, port); /* clock */
145 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
146 outb_p(128+64+16+8+2+1, port); /* clock */
151 static void send_1_byte(int port, struct rt_device *dev)
153 if ((dev->curvol == 0) || (dev->muted)) {
154 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
155 outb_p(128+64+16+4+2+1, port); /* clock */
158 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
159 outb_p(128+64+16+8+4+2+1, port); /* clock */
165 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
169 /* adapted from radio-aztech.c */
171 /* now uses VIDEO_TUNER_LOW for fine tuning */
173 freq += 171200; /* Add 10.7 MHz IF */
174 freq /= 800; /* Convert to 50 kHz units */
176 mutex_lock(&lock); /* Stop other ops interfering */
178 send_0_byte (io, dev); /* 0: LSB of frequency */
180 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
182 send_1_byte (io, dev);
184 send_0_byte (io, dev);
186 send_0_byte (io, dev); /* 14: test bit - always 0 */
187 send_0_byte (io, dev); /* 15: test bit - always 0 */
189 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
190 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
191 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
192 send_0_byte (io, dev); /* 19: time base - always 0 */
194 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
195 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
196 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
197 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
199 if ((dev->curvol == 0) || (dev->muted))
200 outb (0xd0, io); /* volume steady + sigstr */
202 outb (0xd8, io); /* volume steady + sigstr + on */
209 static int rt_getsigstr(struct rt_device *dev)
211 if (inb(io) & 2) /* bit set = no signal present */
213 return 1; /* signal present */
216 static struct v4l2_queryctrl radio_qctrl[] = {
218 .id = V4L2_CID_AUDIO_MUTE,
223 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .id = V4L2_CID_AUDIO_VOLUME,
230 .default_value = 0xff,
231 .type = V4L2_CTRL_TYPE_INTEGER,
235 static int vidioc_querycap(struct file *file, void *priv,
236 struct v4l2_capability *v)
238 strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
239 strlcpy(v->card, "RadioTrack", sizeof(v->card));
240 sprintf(v->bus_info, "ISA");
241 v->version = RADIO_VERSION;
242 v->capabilities = V4L2_CAP_TUNER;
246 static int vidioc_g_tuner(struct file *file, void *priv,
247 struct v4l2_tuner *v)
249 struct rt_device *rt = video_drvdata(file);
254 strcpy(v->name, "FM");
255 v->type = V4L2_TUNER_RADIO;
256 v->rangelow = (87*16000);
257 v->rangehigh = (108*16000);
258 v->rxsubchans = V4L2_TUNER_SUB_MONO;
259 v->capability = V4L2_TUNER_CAP_LOW;
260 v->audmode = V4L2_TUNER_MODE_MONO;
261 v->signal = 0xffff*rt_getsigstr(rt);
265 static int vidioc_s_tuner(struct file *file, void *priv,
266 struct v4l2_tuner *v)
273 static int vidioc_s_frequency(struct file *file, void *priv,
274 struct v4l2_frequency *f)
276 struct rt_device *rt = video_drvdata(file);
278 rt->curfreq = f->frequency;
279 rt_setfreq(rt, rt->curfreq);
283 static int vidioc_g_frequency(struct file *file, void *priv,
284 struct v4l2_frequency *f)
286 struct rt_device *rt = video_drvdata(file);
288 f->type = V4L2_TUNER_RADIO;
289 f->frequency = rt->curfreq;
293 static int vidioc_queryctrl(struct file *file, void *priv,
294 struct v4l2_queryctrl *qc)
298 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
299 if (qc->id && qc->id == radio_qctrl[i].id) {
300 memcpy(qc, &(radio_qctrl[i]),
308 static int vidioc_g_ctrl(struct file *file, void *priv,
309 struct v4l2_control *ctrl)
311 struct rt_device *rt = video_drvdata(file);
314 case V4L2_CID_AUDIO_MUTE:
315 ctrl->value = rt->muted;
317 case V4L2_CID_AUDIO_VOLUME:
318 ctrl->value = rt->curvol * 6554;
324 static int vidioc_s_ctrl(struct file *file, void *priv,
325 struct v4l2_control *ctrl)
327 struct rt_device *rt = video_drvdata(file);
330 case V4L2_CID_AUDIO_MUTE:
334 rt_setvol(rt,rt->curvol);
336 case V4L2_CID_AUDIO_VOLUME:
337 rt_setvol(rt,ctrl->value);
343 static int vidioc_g_audio (struct file *file, void *priv,
344 struct v4l2_audio *a)
349 strcpy(a->name, "Radio");
350 a->capability = V4L2_AUDCAP_STEREO;
354 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
360 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
367 static int vidioc_s_audio(struct file *file, void *priv,
368 struct v4l2_audio *a)
375 static struct rt_device rtrack_unit;
377 static int rtrack_exclusive_open(struct file *file)
379 return test_and_set_bit(0, &rtrack_unit.in_use) ? -EBUSY : 0;
382 static int rtrack_exclusive_release(struct file *file)
384 clear_bit(0, &rtrack_unit.in_use);
388 static const struct v4l2_file_operations rtrack_fops = {
389 .owner = THIS_MODULE,
390 .open = rtrack_exclusive_open,
391 .release = rtrack_exclusive_release,
392 .ioctl = video_ioctl2,
395 static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
396 .vidioc_querycap = vidioc_querycap,
397 .vidioc_g_tuner = vidioc_g_tuner,
398 .vidioc_s_tuner = vidioc_s_tuner,
399 .vidioc_g_audio = vidioc_g_audio,
400 .vidioc_s_audio = vidioc_s_audio,
401 .vidioc_g_input = vidioc_g_input,
402 .vidioc_s_input = vidioc_s_input,
403 .vidioc_g_frequency = vidioc_g_frequency,
404 .vidioc_s_frequency = vidioc_s_frequency,
405 .vidioc_queryctrl = vidioc_queryctrl,
406 .vidioc_g_ctrl = vidioc_g_ctrl,
407 .vidioc_s_ctrl = vidioc_s_ctrl,
410 static struct video_device rtrack_radio = {
411 .name = "RadioTrack radio",
412 .fops = &rtrack_fops,
413 .ioctl_ops = &rtrack_ioctl_ops,
414 .release = video_device_release_empty,
417 static int __init rtrack_init(void)
421 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
425 if (!request_region(io, 2, "rtrack"))
427 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
431 video_set_drvdata(&rtrack_radio, &rtrack_unit);
433 if (video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
434 release_region(io, 2);
437 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
439 /* Set up the I/O locking */
443 /* mute card - prevents noisy bootups */
445 /* this ensures that the volume is all the way down */
446 outb(0x48, io); /* volume down but still "on" */
447 sleep_delay(2000000); /* make sure it's totally down */
448 outb(0xc0, io); /* steady volume, mute card */
449 rtrack_unit.curvol = 0;
454 MODULE_AUTHOR("M.Kirkwood");
455 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
456 MODULE_LICENSE("GPL");
458 module_param(io, int, 0);
459 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
460 module_param(radio_nr, int, 0);
462 static void __exit cleanup_rtrack_module(void)
464 video_unregister_device(&rtrack_radio);
465 release_region(io,2);
468 module_init(rtrack_init);
469 module_exit(cleanup_rtrack_module);