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>
44 #include <media/v4l2-ctrls.h>
45 #include <media/v4l2-fh.h>
46 #include <media/v4l2-event.h>
48 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
49 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION("0.3.4");
53 static int io = -1; /* default to isapnp activation */
54 static int radio_nr = -1;
56 module_param(io, int, 0);
57 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
58 module_param(radio_nr, int, 0);
60 #define RDS_BUFFER 256
65 struct v4l2_device v4l2_dev;
66 struct video_device vdev;
67 struct v4l2_ctrl_handler ctrl_handler;
72 wait_queue_head_t read_queue;
73 struct timer_list readtimer;
74 __u8 rdsin, rdsout, rdsstat;
75 unsigned char rdsbuf[RDS_BUFFER];
80 static struct cadet cadet_card;
83 * Signal Strength Threshold Values
84 * The V4L API spec does not define any particular unit for the signal
85 * strength value. These values are in microvolts of RF at the tuner's input.
87 static __u16 sigtable[2][4] = {
93 static int cadet_getstereo(struct cadet *dev)
95 int ret = V4L2_TUNER_SUB_MONO;
97 if (dev->curtuner != 0) /* Only FM has stereo capability! */
98 return V4L2_TUNER_SUB_MONO;
100 outb(7, dev->io); /* Select tuner control */
101 if ((inb(dev->io + 1) & 0x40) == 0)
102 ret = V4L2_TUNER_SUB_STEREO;
106 static unsigned cadet_gettune(struct cadet *dev)
115 outb(7, dev->io); /* Select tuner control */
116 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
117 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
118 dev->tunestat = 0xffff;
121 * Read the shift register
123 for (i = 0; i < 25; i++) {
124 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
126 outb(0x01, dev->io + 1);
127 dev->tunestat &= inb(dev->io + 1);
128 outb(0x00, dev->io + 1);
133 * Restore volume/mute setting
135 outb(curvol, dev->io + 1);
139 static unsigned cadet_getfreq(struct cadet *dev)
142 unsigned freq = 0, test, fifo = 0;
145 * Read current tuning
147 fifo = cadet_gettune(dev);
150 * Convert to actual frequency
152 if (dev->curtuner == 0) { /* FM */
154 for (i = 0; i < 14; i++) {
155 if ((fifo & 0x01) != 0)
160 freq -= 10700000; /* IF frequency is 10.7 MHz */
161 freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
163 if (dev->curtuner == 1) /* AM */
164 freq = ((fifo & 0x7fff) - 2010) * 16;
169 static void cadet_settune(struct cadet *dev, unsigned fifo)
174 outb(7, dev->io); /* Select tuner control */
176 * Write the shift register
179 test = (fifo >> 23) & 0x02; /* Align data for SDO */
180 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
181 outb(7, dev->io); /* Select tuner control */
182 outb(test, dev->io + 1); /* Initialize for write */
183 for (i = 0; i < 25; i++) {
184 test |= 0x01; /* Toggle SCK High */
185 outb(test, dev->io + 1);
186 test &= 0xfe; /* Toggle SCK Low */
187 outb(test, dev->io + 1);
188 fifo = fifo << 1; /* Prepare the next bit */
189 test = 0x1c | ((fifo >> 23) & 0x02);
190 outb(test, dev->io + 1);
194 static void cadet_setfreq(struct cadet *dev, unsigned freq)
201 * Formulate a fifo command
204 if (dev->curtuner == 0) { /* FM */
206 freq = freq / 16; /* Make it kHz */
207 freq += 10700; /* IF is 10700 kHz */
208 for (i = 0; i < 14; i++) {
217 if (dev->curtuner == 1) { /* AM */
218 fifo = (freq / 16) + 2010; /* Make it kHz */
219 fifo |= 0x100000; /* Select AM Band */
223 * Save current volume/mute setting
226 outb(7, dev->io); /* Select tuner control */
227 curvol = inb(dev->io + 1);
232 for (j = 3; j > -1; j--) {
233 cadet_settune(dev, fifo | (j << 16));
235 outb(7, dev->io); /* Select tuner control */
236 outb(curvol, dev->io + 1);
241 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
242 dev->sigstrength = sigtable[dev->curtuner][j];
246 dev->sigstrength = 0;
250 static void cadet_handler(unsigned long data)
252 struct cadet *dev = (void *)data;
254 /* Service the RDS fifo */
255 if (mutex_trylock(&dev->lock)) {
256 outb(0x3, dev->io); /* Select RDS Decoder Control */
257 if ((inb(dev->io + 1) & 0x20) != 0)
258 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
259 outb(0x80, dev->io); /* Select RDS fifo */
260 while ((inb(dev->io) & 0x80) != 0) {
261 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
262 if (dev->rdsin == dev->rdsout)
263 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
267 mutex_unlock(&dev->lock);
271 * Service pending read
273 if (dev->rdsin != dev->rdsout)
274 wake_up_interruptible(&dev->read_queue);
279 init_timer(&dev->readtimer);
280 dev->readtimer.function = cadet_handler;
281 dev->readtimer.data = (unsigned long)0;
282 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
283 add_timer(&dev->readtimer);
287 static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
289 struct cadet *dev = video_drvdata(file);
290 unsigned char readbuf[RDS_BUFFER];
293 mutex_lock(&dev->lock);
294 if (dev->rdsstat == 0) {
296 outb(0x80, dev->io); /* Select RDS fifo */
297 init_timer(&dev->readtimer);
298 dev->readtimer.function = cadet_handler;
299 dev->readtimer.data = (unsigned long)dev;
300 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
301 add_timer(&dev->readtimer);
303 if (dev->rdsin == dev->rdsout) {
304 if (file->f_flags & O_NONBLOCK) {
308 interruptible_sleep_on(&dev->read_queue);
310 while (i < count && dev->rdsin != dev->rdsout)
311 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
313 if (copy_to_user(data, readbuf, i))
316 mutex_unlock(&dev->lock);
321 static int vidioc_querycap(struct file *file, void *priv,
322 struct v4l2_capability *v)
324 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
325 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
326 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
327 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
328 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
329 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
333 static int vidioc_g_tuner(struct file *file, void *priv,
334 struct v4l2_tuner *v)
336 struct cadet *dev = video_drvdata(file);
338 v->type = V4L2_TUNER_RADIO;
341 strlcpy(v->name, "FM", sizeof(v->name));
342 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
343 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW;
344 v->rangelow = 1400000; /* 87.5 MHz */
345 v->rangehigh = 1728000; /* 108.0 MHz */
346 v->rxsubchans = cadet_getstereo(dev);
347 v->audmode = V4L2_TUNER_MODE_STEREO;
348 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
351 strlcpy(v->name, "AM", sizeof(v->name));
352 v->capability = V4L2_TUNER_CAP_LOW;
353 v->rangelow = 8320; /* 520 kHz */
354 v->rangehigh = 26400; /* 1650 kHz */
355 v->rxsubchans = V4L2_TUNER_SUB_MONO;
356 v->audmode = V4L2_TUNER_MODE_MONO;
361 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
365 static int vidioc_s_tuner(struct file *file, void *priv,
366 struct v4l2_tuner *v)
368 if (v->index != 0 && v->index != 1)
373 static int vidioc_g_frequency(struct file *file, void *priv,
374 struct v4l2_frequency *f)
376 struct cadet *dev = video_drvdata(file);
380 f->type = V4L2_TUNER_RADIO;
381 f->frequency = cadet_getfreq(dev);
386 static int vidioc_s_frequency(struct file *file, void *priv,
387 struct v4l2_frequency *f)
389 struct cadet *dev = video_drvdata(file);
391 if (f->type != V4L2_TUNER_RADIO)
394 if (f->frequency < 1400000)
395 f->frequency = 1400000;
396 else if (f->frequency > 1728000)
397 f->frequency = 1728000;
398 } else if (f->tuner == 1) {
399 if (f->frequency < 8320)
401 else if (f->frequency > 26400)
402 f->frequency = 26400;
405 cadet_setfreq(dev, f->frequency);
409 static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
411 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
414 case V4L2_CID_AUDIO_MUTE:
415 outb(7, dev->io); /* Select tuner control */
417 outb(0x00, dev->io + 1);
419 outb(0x20, dev->io + 1);
425 static int cadet_open(struct file *file)
427 struct cadet *dev = video_drvdata(file);
430 mutex_lock(&dev->lock);
431 err = v4l2_fh_open(file);
434 if (v4l2_fh_is_singular_file(file))
435 init_waitqueue_head(&dev->read_queue);
437 mutex_unlock(&dev->lock);
441 static int cadet_release(struct file *file)
443 struct cadet *dev = video_drvdata(file);
445 mutex_lock(&dev->lock);
446 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
447 del_timer_sync(&dev->readtimer);
450 v4l2_fh_release(file);
451 mutex_unlock(&dev->lock);
455 static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
457 struct cadet *dev = video_drvdata(file);
458 unsigned int res = v4l2_ctrl_poll(file, wait);
460 poll_wait(file, &dev->read_queue, wait);
461 if (dev->rdsin != dev->rdsout)
462 res |= POLLIN | POLLRDNORM;
467 static const struct v4l2_file_operations cadet_fops = {
468 .owner = THIS_MODULE,
470 .release = cadet_release,
472 .unlocked_ioctl = video_ioctl2,
476 static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
477 .vidioc_querycap = vidioc_querycap,
478 .vidioc_g_tuner = vidioc_g_tuner,
479 .vidioc_s_tuner = vidioc_s_tuner,
480 .vidioc_g_frequency = vidioc_g_frequency,
481 .vidioc_s_frequency = vidioc_s_frequency,
482 .vidioc_log_status = v4l2_ctrl_log_status,
483 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
484 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
487 static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
488 .s_ctrl = cadet_s_ctrl,
493 static struct pnp_device_id cadet_pnp_devices[] = {
494 /* ADS Cadet AM/FM Radio Card */
495 {.id = "MSM0c24", .driver_data = 0},
499 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
501 static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
505 /* only support one device */
509 if (!pnp_port_valid(dev, 0))
512 io = pnp_port_start(dev, 0);
514 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
519 static struct pnp_driver cadet_pnp_driver = {
520 .name = "radio-cadet",
521 .id_table = cadet_pnp_devices,
522 .probe = cadet_pnp_probe,
527 static struct pnp_driver cadet_pnp_driver;
530 static void cadet_probe(struct cadet *dev)
532 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
535 for (i = 0; i < 8; i++) {
537 if (request_region(dev->io, 2, "cadet-probe")) {
538 cadet_setfreq(dev, 1410);
539 if (cadet_getfreq(dev) == 1410) {
540 release_region(dev->io, 2);
543 release_region(dev->io, 2);
550 * io should only be set if the user has used something like
551 * isapnp (the userspace program) to initialize this card for us
554 static int __init cadet_init(void)
556 struct cadet *dev = &cadet_card;
557 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
558 struct v4l2_ctrl_handler *hdl;
561 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
562 mutex_init(&dev->lock);
564 /* If a probe was requested then probe ISAPnP first (safest) */
566 pnp_register_driver(&cadet_pnp_driver);
569 /* If that fails then probe unsafely if probe is requested */
573 /* Else we bail out */
576 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
577 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
581 if (!request_region(dev->io, 2, "cadet"))
584 res = v4l2_device_register(NULL, v4l2_dev);
586 release_region(dev->io, 2);
587 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
591 hdl = &dev->ctrl_handler;
592 v4l2_ctrl_handler_init(hdl, 2);
593 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
594 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
595 v4l2_dev->ctrl_handler = hdl;
598 v4l2_err(v4l2_dev, "Could not register controls\n");
602 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
603 dev->vdev.v4l2_dev = v4l2_dev;
604 dev->vdev.fops = &cadet_fops;
605 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
606 dev->vdev.release = video_device_release_empty;
607 dev->vdev.lock = &dev->lock;
608 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
609 video_set_drvdata(&dev->vdev, dev);
611 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
613 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
616 v4l2_ctrl_handler_free(hdl);
617 v4l2_device_unregister(v4l2_dev);
618 release_region(dev->io, 2);
620 pnp_unregister_driver(&cadet_pnp_driver);
624 static void __exit cadet_exit(void)
626 struct cadet *dev = &cadet_card;
628 video_unregister_device(&dev->vdev);
629 v4l2_ctrl_handler_free(&dev->ctrl_handler);
630 v4l2_device_unregister(&dev->v4l2_dev);
631 release_region(dev->io, 2);
632 pnp_unregister_driver(&cadet_pnp_driver);
635 module_init(cadet_init);
636 module_exit(cadet_exit);