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/module.h> /* Modules */
34 #include <linux/init.h> /* Initdata */
35 #include <linux/ioport.h> /* request_region */
36 #include <linux/delay.h> /* udelay */
37 #include <linux/videodev2.h> /* V4L2 API defs */
38 #include <linux/param.h>
39 #include <linux/pnp.h>
40 #include <linux/sched.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");
48 MODULE_VERSION("0.3.4");
50 static int io = -1; /* default to isapnp activation */
51 static int radio_nr = -1;
53 module_param(io, int, 0);
54 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
55 module_param(radio_nr, int, 0);
57 #define RDS_BUFFER 256
62 struct v4l2_device v4l2_dev;
63 struct video_device vdev;
69 wait_queue_head_t read_queue;
70 struct timer_list readtimer;
71 __u8 rdsin, rdsout, rdsstat;
72 unsigned char rdsbuf[RDS_BUFFER];
77 static struct cadet cadet_card;
80 * Signal Strength Threshold Values
81 * The V4L API spec does not define any particular unit for the signal
82 * strength value. These values are in microvolts of RF at the tuner's input.
84 static __u16 sigtable[2][4] = {
90 static int cadet_getstereo(struct cadet *dev)
92 int ret = V4L2_TUNER_SUB_MONO;
94 if (dev->curtuner != 0) /* Only FM has stereo capability! */
95 return V4L2_TUNER_SUB_MONO;
97 mutex_lock(&dev->lock);
98 outb(7, dev->io); /* Select tuner control */
99 if ((inb(dev->io + 1) & 0x40) == 0)
100 ret = V4L2_TUNER_SUB_STEREO;
101 mutex_unlock(&dev->lock);
105 static unsigned cadet_gettune(struct cadet *dev)
114 mutex_lock(&dev->lock);
116 outb(7, dev->io); /* Select tuner control */
117 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
118 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
119 dev->tunestat = 0xffff;
122 * Read the shift register
124 for (i = 0; i < 25; i++) {
125 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
127 outb(0x01, dev->io + 1);
128 dev->tunestat &= inb(dev->io + 1);
129 outb(0x00, dev->io + 1);
134 * Restore volume/mute setting
136 outb(curvol, dev->io + 1);
137 mutex_unlock(&dev->lock);
142 static unsigned cadet_getfreq(struct cadet *dev)
145 unsigned freq = 0, test, fifo = 0;
148 * Read current tuning
150 fifo = cadet_gettune(dev);
153 * Convert to actual frequency
155 if (dev->curtuner == 0) { /* FM */
157 for (i = 0; i < 14; i++) {
158 if ((fifo & 0x01) != 0)
163 freq -= 10700000; /* IF frequency is 10.7 MHz */
164 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
166 if (dev->curtuner == 1) /* AM */
167 freq = ((fifo & 0x7fff) - 2010) * 16;
172 static void cadet_settune(struct cadet *dev, unsigned fifo)
177 mutex_lock(&dev->lock);
179 outb(7, dev->io); /* Select tuner control */
181 * Write the shift register
184 test = (fifo >> 23) & 0x02; /* Align data for SDO */
185 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
186 outb(7, dev->io); /* Select tuner control */
187 outb(test, dev->io + 1); /* Initialize for write */
188 for (i = 0; i < 25; i++) {
189 test |= 0x01; /* Toggle SCK High */
190 outb(test, dev->io + 1);
191 test &= 0xfe; /* Toggle SCK Low */
192 outb(test, dev->io + 1);
193 fifo = fifo << 1; /* Prepare the next bit */
194 test = 0x1c | ((fifo >> 23) & 0x02);
195 outb(test, dev->io + 1);
197 mutex_unlock(&dev->lock);
200 static void cadet_setfreq(struct cadet *dev, unsigned freq)
207 * Formulate a fifo command
210 if (dev->curtuner == 0) { /* FM */
212 freq = (freq * 1000) / 16; /* Make it kHz */
213 freq += 10700; /* IF is 10700 kHz */
214 for (i = 0; i < 14; i++) {
223 if (dev->curtuner == 1) { /* AM */
224 fifo = (freq / 16) + 2010; /* Make it kHz */
225 fifo |= 0x100000; /* Select AM Band */
229 * Save current volume/mute setting
232 mutex_lock(&dev->lock);
233 outb(7, dev->io); /* Select tuner control */
234 curvol = inb(dev->io + 1);
235 mutex_unlock(&dev->lock);
240 for (j = 3; j > -1; j--) {
241 cadet_settune(dev, fifo | (j << 16));
243 mutex_lock(&dev->lock);
244 outb(7, dev->io); /* Select tuner control */
245 outb(curvol, dev->io + 1);
246 mutex_unlock(&dev->lock);
251 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
252 dev->sigstrength = sigtable[dev->curtuner][j];
256 dev->sigstrength = 0;
260 static int cadet_getvol(struct cadet *dev)
264 mutex_lock(&dev->lock);
266 outb(7, dev->io); /* Select tuner control */
267 if ((inb(dev->io + 1) & 0x20) != 0)
270 mutex_unlock(&dev->lock);
275 static void cadet_setvol(struct cadet *dev, int vol)
277 mutex_lock(&dev->lock);
278 outb(7, dev->io); /* Select tuner control */
280 outb(0x20, dev->io + 1);
282 outb(0x00, dev->io + 1);
283 mutex_unlock(&dev->lock);
286 static void cadet_handler(unsigned long data)
288 struct cadet *dev = (void *)data;
290 /* Service the RDS fifo */
291 if (mutex_trylock(&dev->lock)) {
292 outb(0x3, dev->io); /* Select RDS Decoder Control */
293 if ((inb(dev->io + 1) & 0x20) != 0)
294 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
295 outb(0x80, dev->io); /* Select RDS fifo */
296 while ((inb(dev->io) & 0x80) != 0) {
297 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
298 if (dev->rdsin == dev->rdsout)
299 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
303 mutex_unlock(&dev->lock);
307 * Service pending read
309 if (dev->rdsin != dev->rdsout)
310 wake_up_interruptible(&dev->read_queue);
315 init_timer(&dev->readtimer);
316 dev->readtimer.function = cadet_handler;
317 dev->readtimer.data = (unsigned long)0;
318 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
319 add_timer(&dev->readtimer);
323 static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
325 struct cadet *dev = video_drvdata(file);
326 unsigned char readbuf[RDS_BUFFER];
329 mutex_lock(&dev->lock);
330 if (dev->rdsstat == 0) {
332 outb(0x80, dev->io); /* Select RDS fifo */
333 init_timer(&dev->readtimer);
334 dev->readtimer.function = cadet_handler;
335 dev->readtimer.data = (unsigned long)dev;
336 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
337 add_timer(&dev->readtimer);
339 if (dev->rdsin == dev->rdsout) {
340 mutex_unlock(&dev->lock);
341 if (file->f_flags & O_NONBLOCK)
343 interruptible_sleep_on(&dev->read_queue);
344 mutex_lock(&dev->lock);
346 while (i < count && dev->rdsin != dev->rdsout)
347 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
348 mutex_unlock(&dev->lock);
350 if (copy_to_user(data, readbuf, i))
356 static int vidioc_querycap(struct file *file, void *priv,
357 struct v4l2_capability *v)
359 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
360 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
361 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
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 V4L2_TUNER_CAP_RDS_BLOCK_IO;
378 v->rangelow = 1400; /* 87.5 MHz */
379 v->rangehigh = 1728; /* 108.0 MHz */
380 v->rxsubchans = cadet_getstereo(dev);
381 switch (v->rxsubchans) {
382 case V4L2_TUNER_SUB_MONO:
383 v->audmode = V4L2_TUNER_MODE_MONO;
385 case V4L2_TUNER_SUB_STEREO:
386 v->audmode = V4L2_TUNER_MODE_STEREO;
391 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
394 strlcpy(v->name, "AM", sizeof(v->name));
395 v->capability = V4L2_TUNER_CAP_LOW;
396 v->rangelow = 8320; /* 520 kHz */
397 v->rangehigh = 26400; /* 1650 kHz */
398 v->rxsubchans = V4L2_TUNER_SUB_MONO;
399 v->audmode = V4L2_TUNER_MODE_MONO;
404 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
408 static int vidioc_s_tuner(struct file *file, void *priv,
409 struct v4l2_tuner *v)
411 struct cadet *dev = video_drvdata(file);
413 if (v->index != 0 && v->index != 1)
415 dev->curtuner = v->index;
419 static int vidioc_g_frequency(struct file *file, void *priv,
420 struct v4l2_frequency *f)
422 struct cadet *dev = video_drvdata(file);
424 f->tuner = dev->curtuner;
425 f->type = V4L2_TUNER_RADIO;
426 f->frequency = cadet_getfreq(dev);
431 static int vidioc_s_frequency(struct file *file, void *priv,
432 struct v4l2_frequency *f)
434 struct cadet *dev = video_drvdata(file);
436 if (f->type != V4L2_TUNER_RADIO)
438 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
440 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
442 cadet_setfreq(dev, f->frequency);
446 static int vidioc_queryctrl(struct file *file, void *priv,
447 struct v4l2_queryctrl *qc)
450 case V4L2_CID_AUDIO_MUTE:
451 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
452 case V4L2_CID_AUDIO_VOLUME:
453 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
458 static int vidioc_g_ctrl(struct file *file, void *priv,
459 struct v4l2_control *ctrl)
461 struct cadet *dev = video_drvdata(file);
464 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
465 ctrl->value = (cadet_getvol(dev) == 0);
467 case V4L2_CID_AUDIO_VOLUME:
468 ctrl->value = cadet_getvol(dev);
476 static int vidioc_s_ctrl(struct file *file, void *priv,
477 struct v4l2_control *ctrl)
479 struct cadet *dev = video_drvdata(file);
482 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
484 cadet_setvol(dev, 0);
486 cadet_setvol(dev, 0xffff);
488 case V4L2_CID_AUDIO_VOLUME:
489 cadet_setvol(dev, ctrl->value);
497 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
503 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
505 return i ? -EINVAL : 0;
508 static int vidioc_g_audio(struct file *file, void *priv,
509 struct v4l2_audio *a)
512 strlcpy(a->name, "Radio", sizeof(a->name));
513 a->capability = V4L2_AUDCAP_STEREO;
517 static int vidioc_s_audio(struct file *file, void *priv,
518 struct v4l2_audio *a)
520 return a->index ? -EINVAL : 0;
523 static int cadet_open(struct file *file)
525 struct cadet *dev = video_drvdata(file);
527 mutex_lock(&dev->lock);
530 init_waitqueue_head(&dev->read_queue);
531 mutex_unlock(&dev->lock);
535 static int cadet_release(struct file *file)
537 struct cadet *dev = video_drvdata(file);
539 mutex_lock(&dev->lock);
541 if (0 == dev->users) {
542 del_timer_sync(&dev->readtimer);
545 mutex_unlock(&dev->lock);
549 static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
551 struct cadet *dev = video_drvdata(file);
553 poll_wait(file, &dev->read_queue, wait);
554 if (dev->rdsin != dev->rdsout)
555 return POLLIN | POLLRDNORM;
560 static const struct v4l2_file_operations cadet_fops = {
561 .owner = THIS_MODULE,
563 .release = cadet_release,
565 .unlocked_ioctl = video_ioctl2,
569 static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
570 .vidioc_querycap = vidioc_querycap,
571 .vidioc_g_tuner = vidioc_g_tuner,
572 .vidioc_s_tuner = vidioc_s_tuner,
573 .vidioc_g_frequency = vidioc_g_frequency,
574 .vidioc_s_frequency = vidioc_s_frequency,
575 .vidioc_queryctrl = vidioc_queryctrl,
576 .vidioc_g_ctrl = vidioc_g_ctrl,
577 .vidioc_s_ctrl = vidioc_s_ctrl,
578 .vidioc_g_audio = vidioc_g_audio,
579 .vidioc_s_audio = vidioc_s_audio,
580 .vidioc_g_input = vidioc_g_input,
581 .vidioc_s_input = vidioc_s_input,
586 static struct pnp_device_id cadet_pnp_devices[] = {
587 /* ADS Cadet AM/FM Radio Card */
588 {.id = "MSM0c24", .driver_data = 0},
592 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
594 static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
598 /* only support one device */
602 if (!pnp_port_valid(dev, 0))
605 io = pnp_port_start(dev, 0);
607 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
612 static struct pnp_driver cadet_pnp_driver = {
613 .name = "radio-cadet",
614 .id_table = cadet_pnp_devices,
615 .probe = cadet_pnp_probe,
620 static struct pnp_driver cadet_pnp_driver;
623 static void cadet_probe(struct cadet *dev)
625 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
628 for (i = 0; i < 8; i++) {
630 if (request_region(dev->io, 2, "cadet-probe")) {
631 cadet_setfreq(dev, 1410);
632 if (cadet_getfreq(dev) == 1410) {
633 release_region(dev->io, 2);
636 release_region(dev->io, 2);
643 * io should only be set if the user has used something like
644 * isapnp (the userspace program) to initialize this card for us
647 static int __init cadet_init(void)
649 struct cadet *dev = &cadet_card;
650 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
653 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
654 mutex_init(&dev->lock);
656 /* If a probe was requested then probe ISAPnP first (safest) */
658 pnp_register_driver(&cadet_pnp_driver);
661 /* If that fails then probe unsafely if probe is requested */
665 /* Else we bail out */
668 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
669 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
673 if (!request_region(dev->io, 2, "cadet"))
676 res = v4l2_device_register(NULL, v4l2_dev);
678 release_region(dev->io, 2);
679 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
683 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
684 dev->vdev.v4l2_dev = v4l2_dev;
685 dev->vdev.fops = &cadet_fops;
686 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
687 dev->vdev.release = video_device_release_empty;
688 video_set_drvdata(&dev->vdev, dev);
690 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
691 v4l2_device_unregister(v4l2_dev);
692 release_region(dev->io, 2);
695 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
698 pnp_unregister_driver(&cadet_pnp_driver);
702 static void __exit cadet_exit(void)
704 struct cadet *dev = &cadet_card;
706 video_unregister_device(&dev->vdev);
707 v4l2_device_unregister(&dev->v4l2_dev);
708 release_region(dev->io, 2);
709 pnp_unregister_driver(&cadet_pnp_driver);
712 module_init(cadet_init);
713 module_exit(cadet_exit);