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)
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* check_region, request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev.h> /* kernel radio structs */
24 #include <linux/config.h> /* CONFIG_RADIO_CADET_PORT */
25 #include <linux/param.h>
27 #ifndef CONFIG_RADIO_CADET_PORT
28 #define CONFIG_RADIO_CADET_PORT 0x330
30 #define RDS_BUFFER 256
32 static int io
=CONFIG_RADIO_CADET_PORT
;
34 static int curtuner
=0;
35 static int tunestat
=0;
36 static int sigstrength
=0;
37 static wait_queue_head_t tunerq
,rdsq
,readq
;
38 struct timer_list tunertimer
,rdstimer
,readtimer
;
39 static __u8 rdsin
=0,rdsout
=0,rdsstat
=0;
40 static unsigned char rdsbuf
[RDS_BUFFER
];
41 static int cadet_lock
=0;
44 * Signal Strength Threshold Values
45 * The V4L API spec does not define any particular unit for the signal
46 * strength value. These values are in microvolts of RF at the tuner's input.
48 static __u16 sigtable
[2][4]={{5,10,30,150},{28,40,63,1000}};
52 void cadet_wake(unsigned long qnum
)
55 case 0: /* cadet_setfreq */
58 case 1: /* cadet_getrds */
66 static int cadet_getrds(void)
71 outb(3,io
); /* Select Decoder Control/Status */
72 outb(inb(io
+1)&0x7f,io
+1); /* Reset RDS detection */
74 init_timer(&rdstimer
);
75 rdstimer
.function
=cadet_wake
;
76 rdstimer
.data
=(unsigned long)1;
77 rdstimer
.expires
=jiffies
+(HZ
/10);
78 init_waitqueue_head(&rdsq
);
83 outb(3,io
); /* Select Decoder Control/Status */
84 if((inb(io
+1)&0x80)!=0) {
85 rdsstat
|=VIDEO_TUNER_RDS_ON
;
87 if((inb(io
+1)&0x10)!=0) {
88 rdsstat
|=VIDEO_TUNER_MBS_ON
;
97 static int cadet_getstereo(void)
99 if(curtuner
!=0) { /* Only FM has stereo capability! */
103 outb(7,io
); /* Select tuner control */
104 if((inb(io
+1)&0x40)==0) {
106 return 1; /* Stereo pilot detected */
116 static unsigned cadet_gettune(void)
125 outb(7,io
); /* Select tuner control */
126 curvol
=inb(io
+1); /* Save current volume/mute setting */
127 outb(0x00,io
+1); /* Ensure WRITE-ENABLE is LOW */
131 * Read the shift register
134 fifo
=(fifo
<<1)|((inb(io
+1)>>7)&0x01);
143 * Restore volume/mute setting
153 static unsigned cadet_getfreq(void)
156 unsigned freq
=0,test
,fifo
=0;
159 * Read current tuning
161 fifo
=cadet_gettune();
164 * Convert to actual frequency
166 if(curtuner
==0) { /* FM */
175 freq
-=10700000; /* IF frequency is 10.7 MHz */
176 freq
=(freq
*16)/1000000; /* Make it 1/16 MHz */
178 if(curtuner
==1) { /* AM */
179 freq
=((fifo
&0x7fff)-2010)*16;
187 static void cadet_settune(unsigned fifo
)
193 outb(7,io
); /* Select tuner control */
195 * Write the shift register
198 test
=(fifo
>>23)&0x02; /* Align data for SDO */
199 test
|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
200 outb(7,io
); /* Select tuner control */
201 outb(test
,io
+1); /* Initialize for write */
203 test
|=0x01; /* Toggle SCK High */
205 test
&=0xfe; /* Toggle SCK Low */
207 fifo
=fifo
<<1; /* Prepare the next bit */
208 test
=0x1c|((fifo
>>23)&0x02);
216 static void cadet_setfreq(unsigned freq
)
223 * Formulate a fifo command
226 if(curtuner
==0) { /* FM */
228 freq
=(freq
*1000)/16; /* Make it kHz */
229 freq
+=10700; /* IF is 10700 kHz */
239 if(curtuner
==1) { /* AM */
240 fifo
=(freq
/16)+2010; /* Make it kHz */
241 fifo
|=0x100000; /* Select AM Band */
245 * Save current volume/mute setting
248 outb(7,io
); /* Select tuner control */
255 cadet_settune(fifo
|(j
<<16));
256 outb(7,io
); /* Select tuner control */
259 init_timer(&tunertimer
);
260 tunertimer
.function
=cadet_wake
;
261 tunertimer
.data
=(unsigned long)0;
262 tunertimer
.expires
=jiffies
+(HZ
/10);
263 init_waitqueue_head(&tunerq
);
264 add_timer(&tunertimer
);
267 if((tunestat
&0x40)==0) { /* Tuned */
268 sigstrength
=sigtable
[curtuner
][j
];
278 static int cadet_getvol(void)
281 outb(7,io
); /* Select tuner control */
282 if((inb(io
+1)&0x20)!=0) {
293 static void cadet_setvol(int vol
)
296 outb(7,io
); /* Select tuner control */
308 void cadet_handler(unsigned long data
)
311 * Service the RDS fifo
314 outb(0x3,io
); /* Select RDS Decoder Control */
315 if((inb(io
+1)&0x20)!=0) {
316 printk(KERN_CRIT
"cadet: RDS fifo overflow\n");
318 outb(0x80,io
); /* Select RDS fifo */
319 while((inb(io
)&0x80)!=0) {
320 rdsbuf
[rdsin
++]=inb(io
+1);
322 printk(KERN_CRIT
"cadet: RDS buffer overflow\n");
328 * Service pending read
331 wake_up_interruptible(&readq
);
337 init_timer(&readtimer
);
338 readtimer
.function
=cadet_handler
;
339 readtimer
.data
=(unsigned long)0;
340 readtimer
.expires
=jiffies
+(HZ
/20);
341 add_timer(&readtimer
);
346 static long cadet_read(struct video_device
*v
,char *buf
,unsigned long count
,
350 unsigned char readbuf
[RDS_BUFFER
];
355 outb(0x80,io
); /* Select RDS fifo */
357 init_timer(&readtimer
);
358 readtimer
.function
=cadet_handler
;
359 readtimer
.data
=(unsigned long)0;
360 readtimer
.expires
=jiffies
+(HZ
/20);
361 add_timer(&readtimer
);
367 interruptible_sleep_on(&readq
);
369 while((i
<count
)&&(rdsin
!=rdsout
)) {
370 readbuf
[i
++]=rdsbuf
[rdsout
++];
372 if(copy_to_user(buf
,readbuf
,i
)) {
380 static int cadet_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
387 struct video_capability v
;
388 v
.type
=VID_TYPE_TUNER
;
391 /* No we don't do pictures */
396 strcpy(v
.name
, "ADS Cadet");
397 if(copy_to_user(arg
,&v
,sizeof(v
)))
403 struct video_tuner v
;
404 if(copy_from_user(&v
, arg
,sizeof(v
))!=0) {
407 if((v
.tuner
<0)||(v
.tuner
>1)) {
413 v
.rangelow
=1400; /* 87.5 MHz */
414 v
.rangehigh
=1728; /* 108.0 MHz */
417 v
.mode
|=VIDEO_MODE_AUTO
;
418 v
.signal
=sigstrength
;
419 if(cadet_getstereo()==1) {
420 v
.flags
|=VIDEO_TUNER_STEREO_ON
;
422 v
.flags
|=cadet_getrds();
423 if(copy_to_user(arg
,&v
, sizeof(v
))) {
429 v
.rangelow
=8320; /* 520 kHz */
430 v
.rangehigh
=26400; /* 1650 kHz */
432 v
.flags
|=VIDEO_TUNER_LOW
;
434 v
.mode
|=VIDEO_MODE_AUTO
;
435 v
.signal
=sigstrength
;
436 if(copy_to_user(arg
,&v
, sizeof(v
))) {
445 struct video_tuner v
;
446 if(copy_from_user(&v
, arg
, sizeof(v
))) {
449 if((v
.tuner
<0)||(v
.tuner
>1)) {
456 freq
=cadet_getfreq();
457 if(copy_to_user(arg
, &freq
, sizeof(freq
)))
461 if(copy_from_user(&freq
, arg
,sizeof(freq
)))
463 if((curtuner
==0)&&((freq
<1400)||(freq
>1728))) {
466 if((curtuner
==1)&&((freq
<8320)||(freq
>26400))) {
473 struct video_audio v
;
474 memset(&v
,0, sizeof(v
));
475 v
.flags
=VIDEO_AUDIO_MUTABLE
|VIDEO_AUDIO_VOLUME
;
476 if(cadet_getstereo()==0) {
477 v
.mode
=VIDEO_SOUND_MONO
;
480 v
.mode
=VIDEO_SOUND_STEREO
;
482 v
.volume
=cadet_getvol();
484 strcpy(v
.name
, "Radio");
485 if(copy_to_user(arg
,&v
, sizeof(v
)))
491 struct video_audio v
;
492 if(copy_from_user(&v
, arg
, sizeof(v
)))
496 cadet_setvol(v
.volume
);
497 if(v
.flags
&VIDEO_AUDIO_MUTE
)
500 cadet_setvol(0xffff);
509 static int cadet_open(struct video_device
*dev
, int flags
)
515 init_waitqueue_head(&readq
);
519 static void cadet_close(struct video_device
*dev
)
522 del_timer(&readtimer
);
530 static struct video_device cadet_radio
=
538 NULL
, /* Can't write */
546 static int cadet_probe(void)
548 static int iovals
[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
553 if(check_region(io
,2)>=0) {
555 if(cadet_getfreq()==1410) {
564 static int __init
cadet_init(void)
572 printk(KERN_ERR
"You must set an I/O address with io=0x???\n");
576 if (!request_region(io
,2,"cadet"))
578 if(video_register_device(&cadet_radio
,VFL_TYPE_RADIO
)==-1) {
579 release_region(io
,2);
582 printk(KERN_INFO
"ADS Cadet Radio Card at 0x%x\n",io
);
588 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
589 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
590 MODULE_PARM(io
, "i");
591 MODULE_PARM_DESC(io
, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
595 static void __exit
cadet_cleanup_module(void)
597 video_unregister_device(&cadet_radio
);
598 release_region(io
,2);
601 module_init(cadet_init
);
602 module_exit(cadet_cleanup_module
);