Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | /* cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card * * by Fred Gleason <fredg@wava.com> * Version 0.3.2 * * (Loosely) based on code for the Aztech radio card by * * Russell Kroll (rkroll@exploits.org) * Quay Ly * Donald Song * Jason Lewis (jlewis@twilight.vtc.vsc.edu) * Scott McGrath (smcgrath@twilight.vtc.vsc.edu) * William McGrath (wmcgrath@twilight.vtc.vsc.edu) * */ #include <linux/module.h> /* Modules */ #include <linux/init.h> /* Initdata */ #include <linux/ioport.h> /* check_region, request_region */ #include <linux/delay.h> /* udelay */ #include <asm/io.h> /* outb, outb_p */ #include <asm/uaccess.h> /* copy to/from user */ #include <linux/videodev.h> /* kernel radio structs */ #include <linux/config.h> /* CONFIG_RADIO_CADET_PORT */ #include <linux/param.h> #ifndef CONFIG_RADIO_CADET_PORT #define CONFIG_RADIO_CADET_PORT 0x330 #endif #define RDS_BUFFER 256 static int io=CONFIG_RADIO_CADET_PORT; static int users=0; static int curtuner=0; static int tunestat=0; static int sigstrength=0; static wait_queue_head_t tunerq,rdsq,readq; struct timer_list tunertimer,rdstimer,readtimer; static __u8 rdsin=0,rdsout=0,rdsstat=0; static unsigned char rdsbuf[RDS_BUFFER]; static int cadet_lock=0; /* * Signal Strength Threshold Values * The V4L API spec does not define any particular unit for the signal * strength value. These values are in microvolts of RF at the tuner's input. */ static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}}; void cadet_wake(unsigned long qnum) { switch(qnum) { case 0: /* cadet_setfreq */ wake_up(&tunerq); break; case 1: /* cadet_getrds */ wake_up(&rdsq); break; } } static int cadet_getrds(void) { int rdsstat=0; cadet_lock++; outb(3,io); /* Select Decoder Control/Status */ outb(inb(io+1)&0x7f,io+1); /* Reset RDS detection */ cadet_lock--; init_timer(&rdstimer); rdstimer.function=cadet_wake; rdstimer.data=(unsigned long)1; rdstimer.expires=jiffies+(HZ/10); init_waitqueue_head(&rdsq); add_timer(&rdstimer); sleep_on(&rdsq); cadet_lock++; outb(3,io); /* Select Decoder Control/Status */ if((inb(io+1)&0x80)!=0) { rdsstat|=VIDEO_TUNER_RDS_ON; } if((inb(io+1)&0x10)!=0) { rdsstat|=VIDEO_TUNER_MBS_ON; } cadet_lock--; return rdsstat; } static int cadet_getstereo(void) { if(curtuner!=0) { /* Only FM has stereo capability! */ return 0; } cadet_lock++; outb(7,io); /* Select tuner control */ if((inb(io+1)&0x40)==0) { cadet_lock--; return 1; /* Stereo pilot detected */ } else { cadet_lock--; return 0; /* Mono */ } } static unsigned cadet_gettune(void) { int curvol,i; unsigned fifo=0; /* * Prepare for read */ cadet_lock++; outb(7,io); /* Select tuner control */ curvol=inb(io+1); /* Save current volume/mute setting */ outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */ tunestat=0xffff; /* * Read the shift register */ for(i=0;i<25;i++) { fifo=(fifo<<1)|((inb(io+1)>>7)&0x01); if(i<24) { outb(0x01,io+1); tunestat&=inb(io+1); outb(0x00,io+1); } } /* * Restore volume/mute setting */ outb(curvol,io+1); cadet_lock--; return fifo; } static unsigned cadet_getfreq(void) { int i; unsigned freq=0,test,fifo=0; /* * Read current tuning */ fifo=cadet_gettune(); /* * Convert to actual frequency */ if(curtuner==0) { /* FM */ test=12500; for(i=0;i<14;i++) { if((fifo&0x01)!=0) { freq+=test; } test=test<<1; fifo=fifo>>1; } freq-=10700000; /* IF frequency is 10.7 MHz */ freq=(freq*16)/1000000; /* Make it 1/16 MHz */ } if(curtuner==1) { /* AM */ freq=((fifo&0x7fff)-2010)*16; } return freq; } static void cadet_settune(unsigned fifo) { int i; unsigned test; cadet_lock++; outb(7,io); /* Select tuner control */ /* * Write the shift register */ test=0; test=(fifo>>23)&0x02; /* Align data for SDO */ test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */ outb(7,io); /* Select tuner control */ outb(test,io+1); /* Initialize for write */ for(i=0;i<25;i++) { test|=0x01; /* Toggle SCK High */ outb(test,io+1); test&=0xfe; /* Toggle SCK Low */ outb(test,io+1); fifo=fifo<<1; /* Prepare the next bit */ test=0x1c|((fifo>>23)&0x02); outb(test,io+1); } cadet_lock--; } static void cadet_setfreq(unsigned freq) { unsigned fifo; int i,j,test; int curvol; /* * Formulate a fifo command */ fifo=0; if(curtuner==0) { /* FM */ test=102400; freq=(freq*1000)/16; /* Make it kHz */ freq+=10700; /* IF is 10700 kHz */ for(i=0;i<14;i++) { fifo=fifo<<1; if(freq>=test) { fifo|=0x01; freq-=test; } test=test>>1; } } if(curtuner==1) { /* AM */ fifo=(freq/16)+2010; /* Make it kHz */ fifo|=0x100000; /* Select AM Band */ } /* * Save current volume/mute setting */ cadet_lock++; outb(7,io); /* Select tuner control */ curvol=inb(io+1); /* * Tune the card */ for(j=3;j>-1;j--) { cadet_settune(fifo|(j<<16)); outb(7,io); /* Select tuner control */ outb(curvol,io+1); cadet_lock--; init_timer(&tunertimer); tunertimer.function=cadet_wake; tunertimer.data=(unsigned long)0; tunertimer.expires=jiffies+(HZ/10); init_waitqueue_head(&tunerq); add_timer(&tunertimer); sleep_on(&tunerq); cadet_gettune(); if((tunestat&0x40)==0) { /* Tuned */ sigstrength=sigtable[curtuner][j]; return; } cadet_lock++; } cadet_lock--; sigstrength=0; } static int cadet_getvol(void) { cadet_lock++; outb(7,io); /* Select tuner control */ if((inb(io+1)&0x20)!=0) { cadet_lock--; return 0xffff; } else { cadet_lock--; return 0; } } static void cadet_setvol(int vol) { cadet_lock++; outb(7,io); /* Select tuner control */ if(vol>0) { outb(0x20,io+1); } else { outb(0x00,io+1); } cadet_lock--; } void cadet_handler(unsigned long data) { /* * Service the RDS fifo */ if(cadet_lock==0) { outb(0x3,io); /* Select RDS Decoder Control */ if((inb(io+1)&0x20)!=0) { printk(KERN_CRIT "cadet: RDS fifo overflow\n"); } outb(0x80,io); /* Select RDS fifo */ while((inb(io)&0x80)!=0) { rdsbuf[rdsin++]=inb(io+1); if(rdsin==rdsout) { printk(KERN_CRIT "cadet: RDS buffer overflow\n"); } } } /* * Service pending read */ if( rdsin!=rdsout) { wake_up_interruptible(&readq); } /* * Clean up and exit */ init_timer(&readtimer); readtimer.function=cadet_handler; readtimer.data=(unsigned long)0; readtimer.expires=jiffies+(HZ/20); add_timer(&readtimer); } static long cadet_read(struct video_device *v,char *buf,unsigned long count, int nonblock) { int i=0,c; unsigned char readbuf[RDS_BUFFER]; if(rdsstat==0) { cadet_lock++; rdsstat=1; outb(0x80,io); /* Select RDS fifo */ c=3*(inb(io)&0x03); for(i=0;i<c;i++) { /* Flush the fifo */ inb(io+1); } cadet_lock--; init_timer(&readtimer); readtimer.function=cadet_handler; readtimer.data=(unsigned long)0; readtimer.expires=jiffies+(HZ/20); add_timer(&readtimer); } if(rdsin==rdsout) { if(nonblock) { return -EWOULDBLOCK; } interruptible_sleep_on(&readq); } while((i<count)&&(rdsin!=rdsout)) { readbuf[i++]=rdsbuf[rdsout++]; } if(copy_to_user(buf,readbuf,i)) { return -EFAULT; } return i; } static int cadet_ioctl(struct video_device *dev, unsigned int cmd, void *arg) { unsigned freq; switch(cmd) { case VIDIOCGCAP: { struct video_capability v; v.type=VID_TYPE_TUNER; v.channels=2; v.audios=1; /* No we don't do pictures */ v.maxwidth=0; v.maxheight=0; v.minwidth=0; v.minheight=0; strcpy(v.name, "ADS Cadet"); if(copy_to_user(arg,&v,sizeof(v))) return -EFAULT; return 0; } case VIDIOCGTUNER: { struct video_tuner v; if(copy_from_user(&v, arg,sizeof(v))!=0) { return -EFAULT; } if((v.tuner<0)||(v.tuner>1)) { return -EINVAL; } switch(v.tuner) { case 0: strcpy(v.name,"FM"); v.rangelow=1400; /* 87.5 MHz */ v.rangehigh=1728; /* 108.0 MHz */ v.flags=0; v.mode=0; v.mode|=VIDEO_MODE_AUTO; v.signal=sigstrength; if(cadet_getstereo()==1) { v.flags|=VIDEO_TUNER_STEREO_ON; } v.flags|=cadet_getrds(); if(copy_to_user(arg,&v, sizeof(v))) { return -EFAULT; } break; case 1: strcpy(v.name,"AM"); v.rangelow=8320; /* 520 kHz */ v.rangehigh=26400; /* 1650 kHz */ v.flags=0; v.flags|=VIDEO_TUNER_LOW; v.mode=0; v.mode|=VIDEO_MODE_AUTO; v.signal=sigstrength; if(copy_to_user(arg,&v, sizeof(v))) { return -EFAULT; } break; } return 0; } case VIDIOCSTUNER: { struct video_tuner v; if(copy_from_user(&v, arg, sizeof(v))) { return -EFAULT; } if((v.tuner<0)||(v.tuner>1)) { return -EINVAL; } curtuner=v.tuner; return 0; } case VIDIOCGFREQ: freq=cadet_getfreq(); if(copy_to_user(arg, &freq, sizeof(freq))) return -EFAULT; return 0; case VIDIOCSFREQ: if(copy_from_user(&freq, arg,sizeof(freq))) return -EFAULT; if((curtuner==0)&&((freq<1400)||(freq>1728))) { return -EINVAL; } if((curtuner==1)&&((freq<8320)||(freq>26400))) { return -EINVAL; } cadet_setfreq(freq); return 0; case VIDIOCGAUDIO: { struct video_audio v; memset(&v,0, sizeof(v)); v.flags=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME; if(cadet_getstereo()==0) { v.mode=VIDEO_SOUND_MONO; } else { v.mode=VIDEO_SOUND_STEREO; } v.volume=cadet_getvol(); v.step=0xffff; strcpy(v.name, "Radio"); if(copy_to_user(arg,&v, sizeof(v))) return -EFAULT; return 0; } case VIDIOCSAUDIO: { struct video_audio v; if(copy_from_user(&v, arg, sizeof(v))) return -EFAULT; if(v.audio) return -EINVAL; cadet_setvol(v.volume); if(v.flags&VIDEO_AUDIO_MUTE) cadet_setvol(0); else cadet_setvol(0xffff); return 0; } default: return -ENOIOCTLCMD; } } static int cadet_open(struct video_device *dev, int flags) { if(users) return -EBUSY; users++; MOD_INC_USE_COUNT; init_waitqueue_head(&readq); return 0; } static void cadet_close(struct video_device *dev) { if(rdsstat==1) { del_timer(&readtimer); rdsstat=0; } users--; MOD_DEC_USE_COUNT; } static struct video_device cadet_radio= { "Cadet radio", VID_TYPE_TUNER, VID_HARDWARE_CADET, cadet_open, cadet_close, cadet_read, NULL, /* Can't write */ NULL, /* No poll */ cadet_ioctl, NULL, NULL }; __initfunc(int cadet_init(struct video_init *v)) { #ifndef MODULE if(cadet_probe()<0) { return EINVAL; } #endif if(video_register_device(&cadet_radio,VFL_TYPE_RADIO)==-1) return -EINVAL; request_region(io,2,"cadet"); printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io); return 0; } #ifndef MODULE static int cadet_probe(void) { static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e}; int i; for(i=0;i<8;i++) { io=iovals[i]; if(check_region(io,2)>=0) { cadet_setfreq(1410); if(cadet_getfreq()==1410) { return io; } } } return -1; } #endif #ifdef MODULE MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card."); MODULE_PARM(io, "i"); MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)"); EXPORT_NO_SYMBOLS; int init_module(void) { if(io==-1) { printk(KERN_ERR "You must set an I/O address with io=0x???\n"); return -EINVAL; } return cadet_init(NULL); } void cleanup_module(void) { video_unregister_device(&cadet_radio); release_region(io,2); } #endif |