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 <linux/param.h>
43 #include <linux/pnp.h>
45 #define RDS_BUFFER 256
49 #define CADET_VERSION KERNEL_VERSION(0,3,3)
51 static int io=-1; /* default to isapnp activation */
52 static int radio_nr = -1;
54 static int curtuner=0;
55 static int tunestat=0;
56 static int sigstrength=0;
57 static wait_queue_head_t read_queue;
58 static struct timer_list readtimer;
59 static __u8 rdsin=0,rdsout=0,rdsstat=0;
60 static unsigned char rdsbuf[RDS_BUFFER];
61 static spinlock_t cadet_io_lock;
63 static int cadet_probe(void);
66 * Signal Strength Threshold Values
67 * The V4L API spec does not define any particular unit for the signal
68 * strength value. These values are in microvolts of RF at the tuner's input.
70 static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}};
76 int ret = V4L2_TUNER_SUB_MONO;
77 if(curtuner != 0) /* Only FM has stereo capability! */
78 return V4L2_TUNER_SUB_MONO;
80 spin_lock(&cadet_io_lock);
81 outb(7,io); /* Select tuner control */
82 if( (inb(io+1) & 0x40) == 0)
83 ret = V4L2_TUNER_SUB_STEREO;
84 spin_unlock(&cadet_io_lock);
98 spin_lock(&cadet_io_lock);
100 outb(7,io); /* Select tuner control */
101 curvol=inb(io+1); /* Save current volume/mute setting */
102 outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */
106 * Read the shift register
109 fifo=(fifo<<1)|((inb(io+1)>>7)&0x01);
118 * Restore volume/mute setting
121 spin_unlock(&cadet_io_lock);
130 unsigned freq=0,test,fifo=0;
133 * Read current tuning
135 fifo=cadet_gettune();
138 * Convert to actual frequency
140 if(curtuner==0) { /* FM */
149 freq-=10700000; /* IF frequency is 10.7 MHz */
150 freq=(freq*16)/1000000; /* Make it 1/16 MHz */
152 if(curtuner==1) { /* AM */
153 freq=((fifo&0x7fff)-2010)*16;
160 cadet_settune(unsigned fifo)
165 spin_lock(&cadet_io_lock);
167 outb(7,io); /* Select tuner control */
169 * Write the shift register
172 test=(fifo>>23)&0x02; /* Align data for SDO */
173 test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
174 outb(7,io); /* Select tuner control */
175 outb(test,io+1); /* Initialize for write */
177 test|=0x01; /* Toggle SCK High */
179 test&=0xfe; /* Toggle SCK Low */
181 fifo=fifo<<1; /* Prepare the next bit */
182 test=0x1c|((fifo>>23)&0x02);
185 spin_unlock(&cadet_io_lock);
189 cadet_setfreq(unsigned freq)
196 * Formulate a fifo command
199 if(curtuner==0) { /* FM */
201 freq=(freq*1000)/16; /* Make it kHz */
202 freq+=10700; /* IF is 10700 kHz */
212 if(curtuner==1) { /* AM */
213 fifo=(freq/16)+2010; /* Make it kHz */
214 fifo|=0x100000; /* Select AM Band */
218 * Save current volume/mute setting
221 spin_lock(&cadet_io_lock);
222 outb(7,io); /* Select tuner control */
224 spin_unlock(&cadet_io_lock);
230 cadet_settune(fifo|(j<<16));
232 spin_lock(&cadet_io_lock);
233 outb(7,io); /* Select tuner control */
235 spin_unlock(&cadet_io_lock);
240 if((tunestat & 0x40) == 0) { /* Tuned */
241 sigstrength=sigtable[curtuner][j];
254 spin_lock(&cadet_io_lock);
256 outb(7,io); /* Select tuner control */
257 if((inb(io + 1) & 0x20) != 0)
260 spin_unlock(&cadet_io_lock);
266 cadet_setvol(int vol)
268 spin_lock(&cadet_io_lock);
269 outb(7,io); /* Select tuner control */
274 spin_unlock(&cadet_io_lock);
278 cadet_handler(unsigned long data)
281 * Service the RDS fifo
284 if(spin_trylock(&cadet_io_lock))
286 outb(0x3,io); /* Select RDS Decoder Control */
287 if((inb(io+1)&0x20)!=0) {
288 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
290 outb(0x80,io); /* Select RDS fifo */
291 while((inb(io)&0x80)!=0) {
292 rdsbuf[rdsin]=inb(io+1);
294 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
298 spin_unlock(&cadet_io_lock);
302 * Service pending read
305 wake_up_interruptible(&read_queue);
310 init_timer(&readtimer);
311 readtimer.function=cadet_handler;
312 readtimer.data=(unsigned long)0;
313 readtimer.expires=jiffies+(HZ/20);
314 add_timer(&readtimer);
320 cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
323 unsigned char readbuf[RDS_BUFFER];
326 spin_lock(&cadet_io_lock);
328 outb(0x80,io); /* Select RDS fifo */
329 spin_unlock(&cadet_io_lock);
330 init_timer(&readtimer);
331 readtimer.function=cadet_handler;
332 readtimer.data=(unsigned long)0;
333 readtimer.expires=jiffies+(HZ/20);
334 add_timer(&readtimer);
337 if (file->f_flags & O_NONBLOCK)
339 interruptible_sleep_on(&read_queue);
341 while( i<count && rdsin!=rdsout)
342 readbuf[i++]=rdsbuf[rdsout++];
344 if (copy_to_user(data,readbuf,i))
351 static int cadet_do_ioctl(struct inode *inode, struct file *file,
352 unsigned int cmd, void *arg)
356 case VIDIOC_QUERYCAP:
358 struct v4l2_capability *cap = arg;
359 memset(cap,0,sizeof(*cap));
363 cap->version = CADET_VERSION;
364 strcpy(cap->driver, "ADS Cadet");
365 strcpy(cap->card, "ADS Cadet");
370 struct v4l2_tuner *t = arg;
371 memset(t,0,sizeof(*t));
372 t->type = V4L2_TUNER_RADIO;
375 case 0: strcpy(t->name, "FM");
376 t->capability = V4L2_TUNER_CAP_STEREO;
377 t->rangelow = 1400; /* 87.5 MHz */
378 t->rangehigh = 1728; /* 108.0 MHz */
379 t->rxsubchans=cadet_getstereo();
380 switch (t->rxsubchans){
381 case V4L2_TUNER_SUB_MONO:
382 t->audmode = V4L2_TUNER_MODE_MONO;
384 case V4L2_TUNER_SUB_STEREO:
385 t->audmode = V4L2_TUNER_MODE_STEREO;
390 case 1: strcpy(t->name, "AM");
391 t->capability = V4L2_TUNER_CAP_LOW;
392 t->rangelow = 8320; /* 520 kHz */
393 t->rangehigh = 26400; /* 1650 kHz */
394 t->rxsubchans = V4L2_TUNER_SUB_MONO;
395 t->audmode = V4L2_TUNER_MODE_MONO;
401 t->signal = sigstrength; /* We might need to modify scaling of this */
406 struct v4l2_tuner *t = arg;
407 if((t->index != 0)&&(t->index != 1))
413 case VIDIOC_G_FREQUENCY:
415 struct v4l2_frequency *f = arg;
416 memset(f,0,sizeof(*f));
418 f->type = V4L2_TUNER_RADIO;
419 f->frequency = cadet_getfreq();
422 case VIDIOC_S_FREQUENCY:
424 struct v4l2_frequency *f = arg;
425 if (f->type != V4L2_TUNER_RADIO){
428 if((curtuner==0)&&((f->frequency<1400)||(f->frequency>1728))) {
431 if((curtuner==1)&&((f->frequency<8320)||(f->frequency>26400))) {
434 cadet_setfreq(f->frequency);
439 struct v4l2_control *c = arg;
441 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
442 c->value = (cadet_getvol() == 0);
444 case V4L2_CID_AUDIO_VOLUME:
445 c->value = cadet_getvol();
454 struct v4l2_control *c = arg;
456 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
457 if (c->value) cadet_setvol(0);
458 else cadet_setvol(0xffff);
460 case V4L2_CID_AUDIO_VOLUME:
461 cadet_setvol(c->value);
475 cadet_ioctl(struct inode *inode, struct file *file,
476 unsigned int cmd, unsigned long arg)
478 return video_usercopy(inode, file, cmd, arg, cadet_do_ioctl);
482 cadet_open(struct inode *inode, struct file *file)
485 if (1 == users) init_waitqueue_head(&read_queue);
490 cadet_release(struct inode *inode, struct file *file)
494 del_timer_sync(&readtimer);
501 cadet_poll(struct file *file, struct poll_table_struct *wait)
503 poll_wait(file,&read_queue,wait);
505 return POLLIN | POLLRDNORM;
510 static const struct file_operations cadet_fops = {
511 .owner = THIS_MODULE,
513 .release = cadet_release,
515 .ioctl = cadet_ioctl,
517 .compat_ioctl = v4l_compat_ioctl32,
521 static struct video_device cadet_radio=
523 .owner = THIS_MODULE,
524 .name = "Cadet radio",
525 .type = VID_TYPE_TUNER,
529 static struct pnp_device_id cadet_pnp_devices[] = {
530 /* ADS Cadet AM/FM Radio Card */
531 {.id = "MSM0c24", .driver_data = 0},
535 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
537 static int cadet_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
541 /* only support one device */
545 if (!pnp_port_valid(dev, 0)) {
549 io = pnp_port_start(dev, 0);
551 printk ("radio-cadet: PnP reports device at %#x\n", io);
556 static struct pnp_driver cadet_pnp_driver = {
557 .name = "radio-cadet",
558 .id_table = cadet_pnp_devices,
559 .probe = cadet_pnp_probe,
563 static int cadet_probe(void)
565 static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
570 if (request_region(io, 2, "cadet-probe")) {
572 if(cadet_getfreq()==1410) {
573 release_region(io, 2);
576 release_region(io, 2);
583 * io should only be set if the user has used something like
584 * isapnp (the userspace program) to initialize this card for us
587 static int __init cadet_init(void)
589 spin_lock_init(&cadet_io_lock);
592 * If a probe was requested then probe ISAPnP first (safest)
595 pnp_register_driver(&cadet_pnp_driver);
597 * If that fails then probe unsafely if probe is requested
608 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
612 if (!request_region(io,2,"cadet"))
614 if(video_register_device(&cadet_radio,VFL_TYPE_RADIO,radio_nr)==-1) {
615 release_region(io,2);
618 printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io);
621 pnp_unregister_driver(&cadet_pnp_driver);
627 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
628 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
629 MODULE_LICENSE("GPL");
631 module_param(io, int, 0);
632 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
633 module_param(radio_nr, int, 0);
635 static void __exit cadet_cleanup_module(void)
637 video_unregister_device(&cadet_radio);
638 release_region(io,2);
639 pnp_unregister_driver(&cadet_pnp_driver);
642 module_init(cadet_init);
643 module_exit(cadet_cleanup_module);