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@redhat.com>
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 <asm/io.h> /* outb, outb_p */
39 #include <asm/uaccess.h> /* copy to/from user */
40 #include <linux/videodev2.h> /* V4L2 API defs */
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43 #include <linux/param.h>
44 #include <linux/pnp.h>
46 #define RDS_BUFFER 256
50 #define CADET_VERSION KERNEL_VERSION(0,3,3)
52 static struct v4l2_queryctrl radio_qctrl[] = {
54 .id = V4L2_CID_AUDIO_MUTE,
59 .type = V4L2_CTRL_TYPE_BOOLEAN,
61 .id = V4L2_CID_AUDIO_VOLUME,
66 .default_value = 0xff,
67 .type = V4L2_CTRL_TYPE_INTEGER,
71 static int io=-1; /* default to isapnp activation */
72 static int radio_nr = -1;
76 static int sigstrength;
77 static wait_queue_head_t read_queue;
78 static struct timer_list readtimer;
79 static __u8 rdsin, rdsout, rdsstat;
80 static unsigned char rdsbuf[RDS_BUFFER];
81 static spinlock_t cadet_io_lock;
83 static int cadet_probe(void);
86 * Signal Strength Threshold Values
87 * The V4L API spec does not define any particular unit for the signal
88 * strength value. These values are in microvolts of RF at the tuner's input.
90 static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}};
96 int ret = V4L2_TUNER_SUB_MONO;
97 if(curtuner != 0) /* Only FM has stereo capability! */
98 return V4L2_TUNER_SUB_MONO;
100 spin_lock(&cadet_io_lock);
101 outb(7,io); /* Select tuner control */
102 if( (inb(io+1) & 0x40) == 0)
103 ret = V4L2_TUNER_SUB_STEREO;
104 spin_unlock(&cadet_io_lock);
118 spin_lock(&cadet_io_lock);
120 outb(7,io); /* Select tuner control */
121 curvol=inb(io+1); /* Save current volume/mute setting */
122 outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */
126 * Read the shift register
129 fifo=(fifo<<1)|((inb(io+1)>>7)&0x01);
138 * Restore volume/mute setting
141 spin_unlock(&cadet_io_lock);
150 unsigned freq=0,test,fifo=0;
153 * Read current tuning
155 fifo=cadet_gettune();
158 * Convert to actual frequency
160 if(curtuner==0) { /* FM */
169 freq-=10700000; /* IF frequency is 10.7 MHz */
170 freq=(freq*16)/1000000; /* Make it 1/16 MHz */
172 if(curtuner==1) { /* AM */
173 freq=((fifo&0x7fff)-2010)*16;
180 cadet_settune(unsigned fifo)
185 spin_lock(&cadet_io_lock);
187 outb(7,io); /* Select tuner control */
189 * Write the shift register
192 test=(fifo>>23)&0x02; /* Align data for SDO */
193 test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
194 outb(7,io); /* Select tuner control */
195 outb(test,io+1); /* Initialize for write */
197 test|=0x01; /* Toggle SCK High */
199 test&=0xfe; /* Toggle SCK Low */
201 fifo=fifo<<1; /* Prepare the next bit */
202 test=0x1c|((fifo>>23)&0x02);
205 spin_unlock(&cadet_io_lock);
209 cadet_setfreq(unsigned freq)
216 * Formulate a fifo command
219 if(curtuner==0) { /* FM */
221 freq=(freq*1000)/16; /* Make it kHz */
222 freq+=10700; /* IF is 10700 kHz */
232 if(curtuner==1) { /* AM */
233 fifo=(freq/16)+2010; /* Make it kHz */
234 fifo|=0x100000; /* Select AM Band */
238 * Save current volume/mute setting
241 spin_lock(&cadet_io_lock);
242 outb(7,io); /* Select tuner control */
244 spin_unlock(&cadet_io_lock);
250 cadet_settune(fifo|(j<<16));
252 spin_lock(&cadet_io_lock);
253 outb(7,io); /* Select tuner control */
255 spin_unlock(&cadet_io_lock);
260 if((tunestat & 0x40) == 0) { /* Tuned */
261 sigstrength=sigtable[curtuner][j];
274 spin_lock(&cadet_io_lock);
276 outb(7,io); /* Select tuner control */
277 if((inb(io + 1) & 0x20) != 0)
280 spin_unlock(&cadet_io_lock);
286 cadet_setvol(int vol)
288 spin_lock(&cadet_io_lock);
289 outb(7,io); /* Select tuner control */
294 spin_unlock(&cadet_io_lock);
298 cadet_handler(unsigned long data)
301 * Service the RDS fifo
304 if(spin_trylock(&cadet_io_lock))
306 outb(0x3,io); /* Select RDS Decoder Control */
307 if((inb(io+1)&0x20)!=0) {
308 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
310 outb(0x80,io); /* Select RDS fifo */
311 while((inb(io)&0x80)!=0) {
312 rdsbuf[rdsin]=inb(io+1);
314 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
318 spin_unlock(&cadet_io_lock);
322 * Service pending read
325 wake_up_interruptible(&read_queue);
330 init_timer(&readtimer);
331 readtimer.function=cadet_handler;
332 readtimer.data=(unsigned long)0;
333 readtimer.expires=jiffies+msecs_to_jiffies(50);
334 add_timer(&readtimer);
340 cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
343 unsigned char readbuf[RDS_BUFFER];
346 spin_lock(&cadet_io_lock);
348 outb(0x80,io); /* Select RDS fifo */
349 spin_unlock(&cadet_io_lock);
350 init_timer(&readtimer);
351 readtimer.function=cadet_handler;
352 readtimer.data=(unsigned long)0;
353 readtimer.expires=jiffies+msecs_to_jiffies(50);
354 add_timer(&readtimer);
357 if (file->f_flags & O_NONBLOCK)
359 interruptible_sleep_on(&read_queue);
361 while( i<count && rdsin!=rdsout)
362 readbuf[i++]=rdsbuf[rdsout++];
364 if (copy_to_user(data,readbuf,i))
370 static int vidioc_querycap(struct file *file, void *priv,
371 struct v4l2_capability *v)
376 v->version = CADET_VERSION;
377 strcpy(v->driver, "ADS Cadet");
378 strcpy(v->card, "ADS Cadet");
382 static int vidioc_g_tuner(struct file *file, void *priv,
383 struct v4l2_tuner *v)
385 v->type = V4L2_TUNER_RADIO;
388 strcpy(v->name, "FM");
389 v->capability = V4L2_TUNER_CAP_STEREO;
390 v->rangelow = 1400; /* 87.5 MHz */
391 v->rangehigh = 1728; /* 108.0 MHz */
392 v->rxsubchans=cadet_getstereo();
393 switch (v->rxsubchans){
394 case V4L2_TUNER_SUB_MONO:
395 v->audmode = V4L2_TUNER_MODE_MONO;
397 case V4L2_TUNER_SUB_STEREO:
398 v->audmode = V4L2_TUNER_MODE_STEREO;
404 strcpy(v->name, "AM");
405 v->capability = V4L2_TUNER_CAP_LOW;
406 v->rangelow = 8320; /* 520 kHz */
407 v->rangehigh = 26400; /* 1650 kHz */
408 v->rxsubchans = V4L2_TUNER_SUB_MONO;
409 v->audmode = V4L2_TUNER_MODE_MONO;
414 v->signal = sigstrength; /* We might need to modify scaling of this */
418 static int vidioc_s_tuner(struct file *file, void *priv,
419 struct v4l2_tuner *v)
421 if((v->index != 0)&&(v->index != 1))
427 static int vidioc_g_frequency(struct file *file, void *priv,
428 struct v4l2_frequency *f)
431 f->type = V4L2_TUNER_RADIO;
432 f->frequency = cadet_getfreq();
437 static int vidioc_s_frequency(struct file *file, void *priv,
438 struct v4l2_frequency *f)
440 if (f->type != V4L2_TUNER_RADIO)
442 if((curtuner==0)&&((f->frequency<1400)||(f->frequency>1728)))
444 if((curtuner==1)&&((f->frequency<8320)||(f->frequency>26400)))
446 cadet_setfreq(f->frequency);
450 static int vidioc_queryctrl(struct file *file, void *priv,
451 struct v4l2_queryctrl *qc)
455 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
456 if (qc->id && qc->id == radio_qctrl[i].id) {
457 memcpy(qc, &(radio_qctrl[i]),
465 static int vidioc_g_ctrl(struct file *file, void *priv,
466 struct v4l2_control *ctrl)
469 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
470 ctrl->value = (cadet_getvol() == 0);
472 case V4L2_CID_AUDIO_VOLUME:
473 ctrl->value = cadet_getvol();
481 static int vidioc_s_ctrl(struct file *file, void *priv,
482 struct v4l2_control *ctrl)
485 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
489 cadet_setvol(0xffff);
491 case V4L2_CID_AUDIO_VOLUME:
492 cadet_setvol(ctrl->value);
500 static int vidioc_g_audio(struct file *file, void *priv,
501 struct v4l2_audio *a)
505 strcpy(a->name, "Radio");
506 a->capability = V4L2_AUDCAP_STEREO;
510 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
516 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
523 static int vidioc_s_audio(struct file *file, void *priv,
524 struct v4l2_audio *a)
532 cadet_open(struct inode *inode, struct file *file)
535 if (1 == users) init_waitqueue_head(&read_queue);
540 cadet_release(struct inode *inode, struct file *file)
544 del_timer_sync(&readtimer);
551 cadet_poll(struct file *file, struct poll_table_struct *wait)
553 poll_wait(file,&read_queue,wait);
555 return POLLIN | POLLRDNORM;
560 static const struct file_operations cadet_fops = {
561 .owner = THIS_MODULE,
563 .release = cadet_release,
565 .ioctl = video_ioctl2,
568 .compat_ioctl = v4l_compat_ioctl32,
573 static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
574 .vidioc_querycap = vidioc_querycap,
575 .vidioc_g_tuner = vidioc_g_tuner,
576 .vidioc_s_tuner = vidioc_s_tuner,
577 .vidioc_g_frequency = vidioc_g_frequency,
578 .vidioc_s_frequency = vidioc_s_frequency,
579 .vidioc_queryctrl = vidioc_queryctrl,
580 .vidioc_g_ctrl = vidioc_g_ctrl,
581 .vidioc_s_ctrl = vidioc_s_ctrl,
582 .vidioc_g_audio = vidioc_g_audio,
583 .vidioc_s_audio = vidioc_s_audio,
584 .vidioc_g_input = vidioc_g_input,
585 .vidioc_s_input = vidioc_s_input,
588 static struct video_device cadet_radio = {
589 .name = "Cadet radio",
591 .ioctl_ops = &cadet_ioctl_ops,
596 static struct pnp_device_id cadet_pnp_devices[] = {
597 /* ADS Cadet AM/FM Radio Card */
598 {.id = "MSM0c24", .driver_data = 0},
602 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
604 static int cadet_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
608 /* only support one device */
612 if (!pnp_port_valid(dev, 0)) {
616 io = pnp_port_start(dev, 0);
618 printk ("radio-cadet: PnP reports device at %#x\n", io);
623 static struct pnp_driver cadet_pnp_driver = {
624 .name = "radio-cadet",
625 .id_table = cadet_pnp_devices,
626 .probe = cadet_pnp_probe,
631 static struct pnp_driver cadet_pnp_driver;
634 static int cadet_probe(void)
636 static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
641 if (request_region(io, 2, "cadet-probe")) {
643 if(cadet_getfreq()==1410) {
644 release_region(io, 2);
647 release_region(io, 2);
654 * io should only be set if the user has used something like
655 * isapnp (the userspace program) to initialize this card for us
658 static int __init cadet_init(void)
660 spin_lock_init(&cadet_io_lock);
663 * If a probe was requested then probe ISAPnP first (safest)
666 pnp_register_driver(&cadet_pnp_driver);
668 * If that fails then probe unsafely if probe is requested
679 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
683 if (!request_region(io,2,"cadet"))
685 if(video_register_device(&cadet_radio,VFL_TYPE_RADIO,radio_nr)==-1) {
686 release_region(io,2);
689 printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io);
692 pnp_unregister_driver(&cadet_pnp_driver);
698 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
699 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
700 MODULE_LICENSE("GPL");
702 module_param(io, int, 0);
703 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
704 module_param(radio_nr, int, 0);
706 static void __exit cadet_cleanup_module(void)
708 video_unregister_device(&cadet_radio);
709 release_region(io,2);
710 pnp_unregister_driver(&cadet_pnp_driver);
713 module_init(cadet_init);
714 module_exit(cadet_cleanup_module);