GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / radio / radio-cadet.c
blobf9180687b3cb210bee102f672cf24041c4cddbb4
1 /* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
3 * by Fred Gleason <fredg@wava.com>
4 * Version 0.3.3
6 * (Loosely) based on code for the Aztech radio card by
8 * Russell Kroll (rkroll@exploits.org)
9 * Quay Ly
10 * Donald Song
11 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
12 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
15 * History:
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>
30 * Changed API to V4L2
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/sched.h>
42 #include <linux/io.h> /* outb, outb_p */
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-ioctl.h>
46 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
47 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
48 MODULE_LICENSE("GPL");
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 CADET_VERSION KERNEL_VERSION(0, 3, 3)
59 #define RDS_BUFFER 256
60 #define RDS_RX_FLAG 1
61 #define MBS_RX_FLAG 2
63 struct cadet {
64 struct v4l2_device v4l2_dev;
65 struct video_device vdev;
66 int io;
67 int users;
68 int curtuner;
69 int tunestat;
70 int sigstrength;
71 wait_queue_head_t read_queue;
72 struct timer_list readtimer;
73 __u8 rdsin, rdsout, rdsstat;
74 unsigned char rdsbuf[RDS_BUFFER];
75 struct mutex lock;
76 int reading;
79 static struct cadet cadet_card;
82 * Signal Strength Threshold Values
83 * The V4L API spec does not define any particular unit for the signal
84 * strength value. These values are in microvolts of RF at the tuner's input.
86 static __u16 sigtable[2][4] = {
87 { 5, 10, 30, 150 },
88 { 28, 40, 63, 1000 }
92 static int cadet_getstereo(struct cadet *dev)
94 int ret = V4L2_TUNER_SUB_MONO;
96 if (dev->curtuner != 0) /* Only FM has stereo capability! */
97 return V4L2_TUNER_SUB_MONO;
99 mutex_lock(&dev->lock);
100 outb(7, dev->io); /* Select tuner control */
101 if ((inb(dev->io + 1) & 0x40) == 0)
102 ret = V4L2_TUNER_SUB_STEREO;
103 mutex_unlock(&dev->lock);
104 return ret;
107 static unsigned cadet_gettune(struct cadet *dev)
109 int curvol, i;
110 unsigned fifo = 0;
113 * Prepare for read
116 mutex_lock(&dev->lock);
118 outb(7, dev->io); /* Select tuner control */
119 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
120 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
121 dev->tunestat = 0xffff;
124 * Read the shift register
126 for (i = 0; i < 25; i++) {
127 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
128 if (i < 24) {
129 outb(0x01, dev->io + 1);
130 dev->tunestat &= inb(dev->io + 1);
131 outb(0x00, dev->io + 1);
136 * Restore volume/mute setting
138 outb(curvol, dev->io + 1);
139 mutex_unlock(&dev->lock);
141 return fifo;
144 static unsigned cadet_getfreq(struct cadet *dev)
146 int i;
147 unsigned freq = 0, test, fifo = 0;
150 * Read current tuning
152 fifo = cadet_gettune(dev);
155 * Convert to actual frequency
157 if (dev->curtuner == 0) { /* FM */
158 test = 12500;
159 for (i = 0; i < 14; i++) {
160 if ((fifo & 0x01) != 0)
161 freq += test;
162 test = test << 1;
163 fifo = fifo >> 1;
165 freq -= 10700000; /* IF frequency is 10.7 MHz */
166 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
168 if (dev->curtuner == 1) /* AM */
169 freq = ((fifo & 0x7fff) - 2010) * 16;
171 return freq;
174 static void cadet_settune(struct cadet *dev, unsigned fifo)
176 int i;
177 unsigned test;
179 mutex_lock(&dev->lock);
181 outb(7, dev->io); /* Select tuner control */
183 * Write the shift register
185 test = 0;
186 test = (fifo >> 23) & 0x02; /* Align data for SDO */
187 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
188 outb(7, dev->io); /* Select tuner control */
189 outb(test, dev->io + 1); /* Initialize for write */
190 for (i = 0; i < 25; i++) {
191 test |= 0x01; /* Toggle SCK High */
192 outb(test, dev->io + 1);
193 test &= 0xfe; /* Toggle SCK Low */
194 outb(test, dev->io + 1);
195 fifo = fifo << 1; /* Prepare the next bit */
196 test = 0x1c | ((fifo >> 23) & 0x02);
197 outb(test, dev->io + 1);
199 mutex_unlock(&dev->lock);
202 static void cadet_setfreq(struct cadet *dev, unsigned freq)
204 unsigned fifo;
205 int i, j, test;
206 int curvol;
209 * Formulate a fifo command
211 fifo = 0;
212 if (dev->curtuner == 0) { /* FM */
213 test = 102400;
214 freq = (freq * 1000) / 16; /* Make it kHz */
215 freq += 10700; /* IF is 10700 kHz */
216 for (i = 0; i < 14; i++) {
217 fifo = fifo << 1;
218 if (freq >= test) {
219 fifo |= 0x01;
220 freq -= test;
222 test = test >> 1;
225 if (dev->curtuner == 1) { /* AM */
226 fifo = (freq / 16) + 2010; /* Make it kHz */
227 fifo |= 0x100000; /* Select AM Band */
231 * Save current volume/mute setting
234 mutex_lock(&dev->lock);
235 outb(7, dev->io); /* Select tuner control */
236 curvol = inb(dev->io + 1);
237 mutex_unlock(&dev->lock);
240 * Tune the card
242 for (j = 3; j > -1; j--) {
243 cadet_settune(dev, fifo | (j << 16));
245 mutex_lock(&dev->lock);
246 outb(7, dev->io); /* Select tuner control */
247 outb(curvol, dev->io + 1);
248 mutex_unlock(&dev->lock);
250 msleep(100);
252 cadet_gettune(dev);
253 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
254 dev->sigstrength = sigtable[dev->curtuner][j];
255 return;
258 dev->sigstrength = 0;
262 static int cadet_getvol(struct cadet *dev)
264 int ret = 0;
266 mutex_lock(&dev->lock);
268 outb(7, dev->io); /* Select tuner control */
269 if ((inb(dev->io + 1) & 0x20) != 0)
270 ret = 0xffff;
272 mutex_unlock(&dev->lock);
273 return ret;
277 static void cadet_setvol(struct cadet *dev, int vol)
279 mutex_lock(&dev->lock);
280 outb(7, dev->io); /* Select tuner control */
281 if (vol > 0)
282 outb(0x20, dev->io + 1);
283 else
284 outb(0x00, dev->io + 1);
285 mutex_unlock(&dev->lock);
288 static void cadet_handler(unsigned long data)
290 struct cadet *dev = (void *)data;
292 /* Service the RDS fifo */
293 if (mutex_trylock(&dev->lock)) {
294 outb(0x3, dev->io); /* Select RDS Decoder Control */
295 if ((inb(dev->io + 1) & 0x20) != 0)
296 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
297 outb(0x80, dev->io); /* Select RDS fifo */
298 while ((inb(dev->io) & 0x80) != 0) {
299 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
300 if (dev->rdsin == dev->rdsout)
301 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
302 else
303 dev->rdsin++;
305 mutex_unlock(&dev->lock);
309 * Service pending read
311 if (dev->rdsin != dev->rdsout)
312 wake_up_interruptible(&dev->read_queue);
315 * Clean up and exit
317 init_timer(&dev->readtimer);
318 dev->readtimer.function = cadet_handler;
319 dev->readtimer.data = (unsigned long)0;
320 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
321 add_timer(&dev->readtimer);
325 static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
327 struct cadet *dev = video_drvdata(file);
328 unsigned char readbuf[RDS_BUFFER];
329 int i = 0;
331 if (dev->rdsstat == 0) {
332 mutex_lock(&dev->lock);
333 dev->rdsstat = 1;
334 outb(0x80, dev->io); /* Select RDS fifo */
335 mutex_unlock(&dev->lock);
336 init_timer(&dev->readtimer);
337 dev->readtimer.function = cadet_handler;
338 dev->readtimer.data = (unsigned long)dev;
339 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
340 add_timer(&dev->readtimer);
342 if (dev->rdsin == dev->rdsout) {
343 if (file->f_flags & O_NONBLOCK)
344 return -EWOULDBLOCK;
345 interruptible_sleep_on(&dev->read_queue);
347 while (i < count && dev->rdsin != dev->rdsout)
348 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
350 if (copy_to_user(data, readbuf, i))
351 return -EFAULT;
352 return 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->version = CADET_VERSION;
363 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
364 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
365 return 0;
368 static int vidioc_g_tuner(struct file *file, void *priv,
369 struct v4l2_tuner *v)
371 struct cadet *dev = video_drvdata(file);
373 v->type = V4L2_TUNER_RADIO;
374 switch (v->index) {
375 case 0:
376 strlcpy(v->name, "FM", sizeof(v->name));
377 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS;
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;
384 break;
385 case V4L2_TUNER_SUB_STEREO:
386 v->audmode = V4L2_TUNER_MODE_STEREO;
387 break;
388 default:
389 break;
391 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
392 break;
393 case 1:
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;
400 break;
401 default:
402 return -EINVAL;
404 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
405 return 0;
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)
414 return -EINVAL;
415 dev->curtuner = v->index;
416 return 0;
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);
427 return 0;
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)
437 return -EINVAL;
438 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
439 return -EINVAL;
440 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
441 return -EINVAL;
442 cadet_setfreq(dev, f->frequency);
443 return 0;
446 static int vidioc_queryctrl(struct file *file, void *priv,
447 struct v4l2_queryctrl *qc)
449 switch (qc->id) {
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);
455 return -EINVAL;
458 static int vidioc_g_ctrl(struct file *file, void *priv,
459 struct v4l2_control *ctrl)
461 struct cadet *dev = video_drvdata(file);
463 switch (ctrl->id) {
464 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
465 ctrl->value = (cadet_getvol(dev) == 0);
466 break;
467 case V4L2_CID_AUDIO_VOLUME:
468 ctrl->value = cadet_getvol(dev);
469 break;
470 default:
471 return -EINVAL;
473 return 0;
476 static int vidioc_s_ctrl(struct file *file, void *priv,
477 struct v4l2_control *ctrl)
479 struct cadet *dev = video_drvdata(file);
481 switch (ctrl->id){
482 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
483 if (ctrl->value)
484 cadet_setvol(dev, 0);
485 else
486 cadet_setvol(dev, 0xffff);
487 break;
488 case V4L2_CID_AUDIO_VOLUME:
489 cadet_setvol(dev, ctrl->value);
490 break;
491 default:
492 return -EINVAL;
494 return 0;
497 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
499 *i = 0;
500 return 0;
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)
511 a->index = 0;
512 strlcpy(a->name, "Radio", sizeof(a->name));
513 a->capability = V4L2_AUDCAP_STEREO;
514 return 0;
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 dev->users++;
528 if (1 == dev->users)
529 init_waitqueue_head(&dev->read_queue);
530 return 0;
533 static int cadet_release(struct file *file)
535 struct cadet *dev = video_drvdata(file);
537 dev->users--;
538 if (0 == dev->users) {
539 del_timer_sync(&dev->readtimer);
540 dev->rdsstat = 0;
542 return 0;
545 static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
547 struct cadet *dev = video_drvdata(file);
549 poll_wait(file, &dev->read_queue, wait);
550 if (dev->rdsin != dev->rdsout)
551 return POLLIN | POLLRDNORM;
552 return 0;
556 static const struct v4l2_file_operations cadet_fops = {
557 .owner = THIS_MODULE,
558 .open = cadet_open,
559 .release = cadet_release,
560 .read = cadet_read,
561 .ioctl = video_ioctl2,
562 .poll = cadet_poll,
565 static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
566 .vidioc_querycap = vidioc_querycap,
567 .vidioc_g_tuner = vidioc_g_tuner,
568 .vidioc_s_tuner = vidioc_s_tuner,
569 .vidioc_g_frequency = vidioc_g_frequency,
570 .vidioc_s_frequency = vidioc_s_frequency,
571 .vidioc_queryctrl = vidioc_queryctrl,
572 .vidioc_g_ctrl = vidioc_g_ctrl,
573 .vidioc_s_ctrl = vidioc_s_ctrl,
574 .vidioc_g_audio = vidioc_g_audio,
575 .vidioc_s_audio = vidioc_s_audio,
576 .vidioc_g_input = vidioc_g_input,
577 .vidioc_s_input = vidioc_s_input,
580 #ifdef CONFIG_PNP
582 static struct pnp_device_id cadet_pnp_devices[] = {
583 /* ADS Cadet AM/FM Radio Card */
584 {.id = "MSM0c24", .driver_data = 0},
585 {.id = ""}
588 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
590 static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
592 if (!dev)
593 return -ENODEV;
594 /* only support one device */
595 if (io > 0)
596 return -EBUSY;
598 if (!pnp_port_valid(dev, 0))
599 return -ENODEV;
601 io = pnp_port_start(dev, 0);
603 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
605 return io;
608 static struct pnp_driver cadet_pnp_driver = {
609 .name = "radio-cadet",
610 .id_table = cadet_pnp_devices,
611 .probe = cadet_pnp_probe,
612 .remove = NULL,
615 #else
616 static struct pnp_driver cadet_pnp_driver;
617 #endif
619 static void cadet_probe(struct cadet *dev)
621 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
622 int i;
624 for (i = 0; i < 8; i++) {
625 dev->io = iovals[i];
626 if (request_region(dev->io, 2, "cadet-probe")) {
627 cadet_setfreq(dev, 1410);
628 if (cadet_getfreq(dev) == 1410) {
629 release_region(dev->io, 2);
630 return;
632 release_region(dev->io, 2);
635 dev->io = -1;
639 * io should only be set if the user has used something like
640 * isapnp (the userspace program) to initialize this card for us
643 static int __init cadet_init(void)
645 struct cadet *dev = &cadet_card;
646 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
647 int res;
649 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
650 mutex_init(&dev->lock);
652 /* If a probe was requested then probe ISAPnP first (safest) */
653 if (io < 0)
654 pnp_register_driver(&cadet_pnp_driver);
655 dev->io = io;
657 /* If that fails then probe unsafely if probe is requested */
658 if (dev->io < 0)
659 cadet_probe(dev);
661 /* Else we bail out */
662 if (dev->io < 0) {
663 #ifdef MODULE
664 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
665 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
666 #endif
667 goto fail;
669 if (!request_region(dev->io, 2, "cadet"))
670 goto fail;
672 res = v4l2_device_register(NULL, v4l2_dev);
673 if (res < 0) {
674 release_region(dev->io, 2);
675 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
676 goto fail;
679 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
680 dev->vdev.v4l2_dev = v4l2_dev;
681 dev->vdev.fops = &cadet_fops;
682 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
683 dev->vdev.release = video_device_release_empty;
684 video_set_drvdata(&dev->vdev, dev);
686 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
687 v4l2_device_unregister(v4l2_dev);
688 release_region(dev->io, 2);
689 goto fail;
691 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
692 return 0;
693 fail:
694 pnp_unregister_driver(&cadet_pnp_driver);
695 return -ENODEV;
698 static void __exit cadet_exit(void)
700 struct cadet *dev = &cadet_card;
702 video_unregister_device(&dev->vdev);
703 v4l2_device_unregister(&dev->v4l2_dev);
704 release_region(dev->io, 2);
705 pnp_unregister_driver(&cadet_pnp_driver);
708 module_init(cadet_init);
709 module_exit(cadet_exit);