1 /* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
3 * by Fred Gleason <fredg@wava.com>
6 * (Loosely) based on code for the Aztech radio card by
8 * Russell Kroll (rkroll@exploits.org)
11 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
12 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
16 * 2000-04-29 Russell Kroll <rkroll@exploits.org>
17 * Added ISAPnP detection for Linux 2.3/2.4
19 * 2001-01-10 Russell Kroll <rkroll@exploits.org>
20 * Removed dead CONFIG_RADIO_CADET_PORT code
21 * PnP detection on load is now default (no args necessary)
23 * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
24 * Updated to latest pnp code
26 * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
27 * Cleaned up locking, delay code, general odds and ends
29 * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
33 #include <linux/version.h>
34 #include <linux/module.h> /* Modules */
35 #include <linux/init.h> /* Initdata */
36 #include <linux/ioport.h> /* request_region */
37 #include <linux/delay.h> /* udelay */
38 #include <linux/videodev2.h> /* V4L2 API defs */
39 #include <linux/param.h>
40 #include <linux/pnp.h>
41 #include <linux/io.h> /* outb, outb_p */
42 #include <media/v4l2-device.h>
43 #include <media/v4l2-ioctl.h>
45 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
46 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
47 MODULE_LICENSE("GPL");
49 static int io = -1; /* default to isapnp activation */
50 static int radio_nr = -1;
52 module_param(io, int, 0);
53 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
54 module_param(radio_nr, int, 0);
56 #define CADET_VERSION KERNEL_VERSION(0, 3, 3)
58 #define RDS_BUFFER 256
63 struct v4l2_device v4l2_dev;
64 struct video_device vdev;
70 wait_queue_head_t read_queue;
71 struct timer_list readtimer;
72 __u8 rdsin, rdsout, rdsstat;
73 unsigned char rdsbuf[RDS_BUFFER];
78 static struct cadet cadet_card;
81 * Signal Strength Threshold Values
82 * The V4L API spec does not define any particular unit for the signal
83 * strength value. These values are in microvolts of RF at the tuner's input.
85 static __u16 sigtable[2][4] = {
91 static int cadet_getstereo(struct cadet *dev)
93 int ret = V4L2_TUNER_SUB_MONO;
95 if (dev->curtuner != 0) /* Only FM has stereo capability! */
96 return V4L2_TUNER_SUB_MONO;
98 mutex_lock(&dev->lock);
99 outb(7, dev->io); /* Select tuner control */
100 if ((inb(dev->io + 1) & 0x40) == 0)
101 ret = V4L2_TUNER_SUB_STEREO;
102 mutex_unlock(&dev->lock);
106 static unsigned cadet_gettune(struct cadet *dev)
115 mutex_lock(&dev->lock);
117 outb(7, dev->io); /* Select tuner control */
118 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
119 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
120 dev->tunestat = 0xffff;
123 * Read the shift register
125 for (i = 0; i < 25; i++) {
126 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
128 outb(0x01, dev->io + 1);
129 dev->tunestat &= inb(dev->io + 1);
130 outb(0x00, dev->io + 1);
135 * Restore volume/mute setting
137 outb(curvol, dev->io + 1);
138 mutex_unlock(&dev->lock);
143 static unsigned cadet_getfreq(struct cadet *dev)
146 unsigned freq = 0, test, fifo = 0;
149 * Read current tuning
151 fifo = cadet_gettune(dev);
154 * Convert to actual frequency
156 if (dev->curtuner == 0) { /* FM */
158 for (i = 0; i < 14; i++) {
159 if ((fifo & 0x01) != 0)
164 freq -= 10700000; /* IF frequency is 10.7 MHz */
165 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
167 if (dev->curtuner == 1) /* AM */
168 freq = ((fifo & 0x7fff) - 2010) * 16;
173 static void cadet_settune(struct cadet *dev, unsigned fifo)
178 mutex_lock(&dev->lock);
180 outb(7, dev->io); /* Select tuner control */
182 * Write the shift register
185 test = (fifo >> 23) & 0x02; /* Align data for SDO */
186 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
187 outb(7, dev->io); /* Select tuner control */
188 outb(test, dev->io + 1); /* Initialize for write */
189 for (i = 0; i < 25; i++) {
190 test |= 0x01; /* Toggle SCK High */
191 outb(test, dev->io + 1);
192 test &= 0xfe; /* Toggle SCK Low */
193 outb(test, dev->io + 1);
194 fifo = fifo << 1; /* Prepare the next bit */
195 test = 0x1c | ((fifo >> 23) & 0x02);
196 outb(test, dev->io + 1);
198 mutex_unlock(&dev->lock);
201 static void cadet_setfreq(struct cadet *dev, unsigned freq)
208 * Formulate a fifo command
211 if (dev->curtuner == 0) { /* FM */
213 freq = (freq * 1000) / 16; /* Make it kHz */
214 freq += 10700; /* IF is 10700 kHz */
215 for (i = 0; i < 14; i++) {
224 if (dev->curtuner == 1) { /* AM */
225 fifo = (freq / 16) + 2010; /* Make it kHz */
226 fifo |= 0x100000; /* Select AM Band */
230 * Save current volume/mute setting
233 mutex_lock(&dev->lock);
234 outb(7, dev->io); /* Select tuner control */
235 curvol = inb(dev->io + 1);
236 mutex_unlock(&dev->lock);
241 for (j = 3; j > -1; j--) {
242 cadet_settune(dev, fifo | (j << 16));
244 mutex_lock(&dev->lock);
245 outb(7, dev->io); /* Select tuner control */
246 outb(curvol, dev->io + 1);
247 mutex_unlock(&dev->lock);
252 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
253 dev->sigstrength = sigtable[dev->curtuner][j];
257 dev->sigstrength = 0;
261 static int cadet_getvol(struct cadet *dev)
265 mutex_lock(&dev->lock);
267 outb(7, dev->io); /* Select tuner control */
268 if ((inb(dev->io + 1) & 0x20) != 0)
271 mutex_unlock(&dev->lock);
276 static void cadet_setvol(struct cadet *dev, int vol)
278 mutex_lock(&dev->lock);
279 outb(7, dev->io); /* Select tuner control */
281 outb(0x20, dev->io + 1);
283 outb(0x00, dev->io + 1);
284 mutex_unlock(&dev->lock);
287 static void cadet_handler(unsigned long data)
289 struct cadet *dev = (void *)data;
291 /* Service the RDS fifo */
292 if (mutex_trylock(&dev->lock)) {
293 outb(0x3, dev->io); /* Select RDS Decoder Control */
294 if ((inb(dev->io + 1) & 0x20) != 0)
295 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
296 outb(0x80, dev->io); /* Select RDS fifo */
297 while ((inb(dev->io) & 0x80) != 0) {
298 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
299 if (dev->rdsin == dev->rdsout)
300 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
304 mutex_unlock(&dev->lock);
308 * Service pending read
310 if (dev->rdsin != dev->rdsout)
311 wake_up_interruptible(&dev->read_queue);
316 init_timer(&dev->readtimer);
317 dev->readtimer.function = cadet_handler;
318 dev->readtimer.data = (unsigned long)0;
319 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
320 add_timer(&dev->readtimer);
324 static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
326 struct cadet *dev = video_drvdata(file);
327 unsigned char readbuf[RDS_BUFFER];
330 if (dev->rdsstat == 0) {
331 mutex_lock(&dev->lock);
333 outb(0x80, dev->io); /* Select RDS fifo */
334 mutex_unlock(&dev->lock);
335 init_timer(&dev->readtimer);
336 dev->readtimer.function = cadet_handler;
337 dev->readtimer.data = (unsigned long)dev;
338 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
339 add_timer(&dev->readtimer);
341 if (dev->rdsin == dev->rdsout) {
342 if (file->f_flags & O_NONBLOCK)
344 interruptible_sleep_on(&dev->read_queue);
346 while (i < count && dev->rdsin != dev->rdsout)
347 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
349 if (copy_to_user(data, readbuf, i))
355 static int vidioc_querycap(struct file *file, void *priv,
356 struct v4l2_capability *v)
358 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
359 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
360 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
361 v->version = CADET_VERSION;
362 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
363 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
367 static int vidioc_g_tuner(struct file *file, void *priv,
368 struct v4l2_tuner *v)
370 struct cadet *dev = video_drvdata(file);
372 v->type = V4L2_TUNER_RADIO;
375 strlcpy(v->name, "FM", sizeof(v->name));
376 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS;
377 v->rangelow = 1400; /* 87.5 MHz */
378 v->rangehigh = 1728; /* 108.0 MHz */
379 v->rxsubchans = cadet_getstereo(dev);
380 switch (v->rxsubchans) {
381 case V4L2_TUNER_SUB_MONO:
382 v->audmode = V4L2_TUNER_MODE_MONO;
384 case V4L2_TUNER_SUB_STEREO:
385 v->audmode = V4L2_TUNER_MODE_STEREO;
390 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
393 strlcpy(v->name, "AM", sizeof(v->name));
394 v->capability = V4L2_TUNER_CAP_LOW;
395 v->rangelow = 8320; /* 520 kHz */
396 v->rangehigh = 26400; /* 1650 kHz */
397 v->rxsubchans = V4L2_TUNER_SUB_MONO;
398 v->audmode = V4L2_TUNER_MODE_MONO;
403 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
407 static int vidioc_s_tuner(struct file *file, void *priv,
408 struct v4l2_tuner *v)
410 struct cadet *dev = video_drvdata(file);
412 if (v->index != 0 && v->index != 1)
414 dev->curtuner = v->index;
418 static int vidioc_g_frequency(struct file *file, void *priv,
419 struct v4l2_frequency *f)
421 struct cadet *dev = video_drvdata(file);
423 f->tuner = dev->curtuner;
424 f->type = V4L2_TUNER_RADIO;
425 f->frequency = cadet_getfreq(dev);
430 static int vidioc_s_frequency(struct file *file, void *priv,
431 struct v4l2_frequency *f)
433 struct cadet *dev = video_drvdata(file);
435 if (f->type != V4L2_TUNER_RADIO)
437 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
439 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
441 cadet_setfreq(dev, f->frequency);
445 static int vidioc_queryctrl(struct file *file, void *priv,
446 struct v4l2_queryctrl *qc)
449 case V4L2_CID_AUDIO_MUTE:
450 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
451 case V4L2_CID_AUDIO_VOLUME:
452 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
457 static int vidioc_g_ctrl(struct file *file, void *priv,
458 struct v4l2_control *ctrl)
460 struct cadet *dev = video_drvdata(file);
463 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
464 ctrl->value = (cadet_getvol(dev) == 0);
466 case V4L2_CID_AUDIO_VOLUME:
467 ctrl->value = cadet_getvol(dev);
475 static int vidioc_s_ctrl(struct file *file, void *priv,
476 struct v4l2_control *ctrl)
478 struct cadet *dev = video_drvdata(file);
481 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
483 cadet_setvol(dev, 0);
485 cadet_setvol(dev, 0xffff);
487 case V4L2_CID_AUDIO_VOLUME:
488 cadet_setvol(dev, ctrl->value);
496 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
502 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
504 return i ? -EINVAL : 0;
507 static int vidioc_g_audio(struct file *file, void *priv,
508 struct v4l2_audio *a)
511 strlcpy(a->name, "Radio", sizeof(a->name));
512 a->capability = V4L2_AUDCAP_STEREO;
516 static int vidioc_s_audio(struct file *file, void *priv,
517 struct v4l2_audio *a)
519 return a->index ? -EINVAL : 0;
522 static int cadet_open(struct file *file)
524 struct cadet *dev = video_drvdata(file);
528 init_waitqueue_head(&dev->read_queue);
532 static int cadet_release(struct file *file)
534 struct cadet *dev = video_drvdata(file);
537 if (0 == dev->users) {
538 del_timer_sync(&dev->readtimer);
544 static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
546 struct cadet *dev = video_drvdata(file);
548 poll_wait(file, &dev->read_queue, wait);
549 if (dev->rdsin != dev->rdsout)
550 return POLLIN | POLLRDNORM;
555 static const struct v4l2_file_operations cadet_fops = {
556 .owner = THIS_MODULE,
558 .release = cadet_release,
560 .ioctl = video_ioctl2,
564 static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
565 .vidioc_querycap = vidioc_querycap,
566 .vidioc_g_tuner = vidioc_g_tuner,
567 .vidioc_s_tuner = vidioc_s_tuner,
568 .vidioc_g_frequency = vidioc_g_frequency,
569 .vidioc_s_frequency = vidioc_s_frequency,
570 .vidioc_queryctrl = vidioc_queryctrl,
571 .vidioc_g_ctrl = vidioc_g_ctrl,
572 .vidioc_s_ctrl = vidioc_s_ctrl,
573 .vidioc_g_audio = vidioc_g_audio,
574 .vidioc_s_audio = vidioc_s_audio,
575 .vidioc_g_input = vidioc_g_input,
576 .vidioc_s_input = vidioc_s_input,
581 static struct pnp_device_id cadet_pnp_devices[] = {
582 /* ADS Cadet AM/FM Radio Card */
583 {.id = "MSM0c24", .driver_data = 0},
587 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
589 static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
593 /* only support one device */
597 if (!pnp_port_valid(dev, 0))
600 io = pnp_port_start(dev, 0);
602 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
607 static struct pnp_driver cadet_pnp_driver = {
608 .name = "radio-cadet",
609 .id_table = cadet_pnp_devices,
610 .probe = cadet_pnp_probe,
615 static struct pnp_driver cadet_pnp_driver;
618 static void cadet_probe(struct cadet *dev)
620 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
623 for (i = 0; i < 8; i++) {
625 if (request_region(dev->io, 2, "cadet-probe")) {
626 cadet_setfreq(dev, 1410);
627 if (cadet_getfreq(dev) == 1410) {
628 release_region(dev->io, 2);
631 release_region(dev->io, 2);
638 * io should only be set if the user has used something like
639 * isapnp (the userspace program) to initialize this card for us
642 static int __init cadet_init(void)
644 struct cadet *dev = &cadet_card;
645 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
648 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
649 mutex_init(&dev->lock);
651 /* If a probe was requested then probe ISAPnP first (safest) */
653 pnp_register_driver(&cadet_pnp_driver);
656 /* If that fails then probe unsafely if probe is requested */
660 /* Else we bail out */
663 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
664 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
668 if (!request_region(dev->io, 2, "cadet"))
671 res = v4l2_device_register(NULL, v4l2_dev);
673 release_region(dev->io, 2);
674 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
678 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
679 dev->vdev.v4l2_dev = v4l2_dev;
680 dev->vdev.fops = &cadet_fops;
681 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
682 dev->vdev.release = video_device_release_empty;
683 video_set_drvdata(&dev->vdev, dev);
685 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
686 v4l2_device_unregister(v4l2_dev);
687 release_region(dev->io, 2);
690 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
693 pnp_unregister_driver(&cadet_pnp_driver);
697 static void __exit cadet_exit(void)
699 struct cadet *dev = &cadet_card;
701 video_unregister_device(&dev->vdev);
702 v4l2_device_unregister(&dev->v4l2_dev);
703 release_region(dev->io, 2);
704 pnp_unregister_driver(&cadet_pnp_driver);
707 module_init(cadet_init);
708 module_exit(cadet_exit);