radio-cadet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
  2. *
  3. * by Fred Gleason <fredg@wava.com>
  4. * Version 0.3.3
  5. *
  6. * (Loosely) based on code for the Aztech radio card by
  7. *
  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)
  14. *
  15. * History:
  16. * 2000-04-29 Russell Kroll <rkroll@exploits.org>
  17. * Added ISAPnP detection for Linux 2.3/2.4
  18. *
  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)
  22. *
  23. * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
  24. * Updated to latest pnp code
  25. *
  26. * 2003-01-31 Alan Cox <alan@redhat.com>
  27. * Cleaned up locking, delay code, general odds and ends
  28. */
  29. #include <linux/module.h> /* Modules */
  30. #include <linux/init.h> /* Initdata */
  31. #include <linux/ioport.h> /* request_region */
  32. #include <linux/delay.h> /* udelay */
  33. #include <asm/io.h> /* outb, outb_p */
  34. #include <asm/uaccess.h> /* copy to/from user */
  35. #include <linux/videodev.h> /* kernel radio structs */
  36. #include <media/v4l2-common.h>
  37. #include <linux/param.h>
  38. #include <linux/pnp.h>
  39. #define RDS_BUFFER 256
  40. static int io=-1; /* default to isapnp activation */
  41. static int radio_nr = -1;
  42. static int users=0;
  43. static int curtuner=0;
  44. static int tunestat=0;
  45. static int sigstrength=0;
  46. static wait_queue_head_t read_queue;
  47. static struct timer_list readtimer;
  48. static __u8 rdsin=0,rdsout=0,rdsstat=0;
  49. static unsigned char rdsbuf[RDS_BUFFER];
  50. static spinlock_t cadet_io_lock;
  51. static int cadet_probe(void);
  52. /*
  53. * Signal Strength Threshold Values
  54. * The V4L API spec does not define any particular unit for the signal
  55. * strength value. These values are in microvolts of RF at the tuner's input.
  56. */
  57. static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}};
  58. static int cadet_getrds(void)
  59. {
  60. int rdsstat=0;
  61. spin_lock(&cadet_io_lock);
  62. outb(3,io); /* Select Decoder Control/Status */
  63. outb(inb(io+1)&0x7f,io+1); /* Reset RDS detection */
  64. spin_unlock(&cadet_io_lock);
  65. msleep(100);
  66. spin_lock(&cadet_io_lock);
  67. outb(3,io); /* Select Decoder Control/Status */
  68. if((inb(io+1)&0x80)!=0) {
  69. rdsstat|=VIDEO_TUNER_RDS_ON;
  70. }
  71. if((inb(io+1)&0x10)!=0) {
  72. rdsstat|=VIDEO_TUNER_MBS_ON;
  73. }
  74. spin_unlock(&cadet_io_lock);
  75. return rdsstat;
  76. }
  77. static int cadet_getstereo(void)
  78. {
  79. int ret = 0;
  80. if(curtuner != 0) /* Only FM has stereo capability! */
  81. return 0;
  82. spin_lock(&cadet_io_lock);
  83. outb(7,io); /* Select tuner control */
  84. if( (inb(io+1) & 0x40) == 0)
  85. ret = 1;
  86. spin_unlock(&cadet_io_lock);
  87. return ret;
  88. }
  89. static unsigned cadet_gettune(void)
  90. {
  91. int curvol,i;
  92. unsigned fifo=0;
  93. /*
  94. * Prepare for read
  95. */
  96. spin_lock(&cadet_io_lock);
  97. outb(7,io); /* Select tuner control */
  98. curvol=inb(io+1); /* Save current volume/mute setting */
  99. outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */
  100. tunestat=0xffff;
  101. /*
  102. * Read the shift register
  103. */
  104. for(i=0;i<25;i++) {
  105. fifo=(fifo<<1)|((inb(io+1)>>7)&0x01);
  106. if(i<24) {
  107. outb(0x01,io+1);
  108. tunestat&=inb(io+1);
  109. outb(0x00,io+1);
  110. }
  111. }
  112. /*
  113. * Restore volume/mute setting
  114. */
  115. outb(curvol,io+1);
  116. spin_unlock(&cadet_io_lock);
  117. return fifo;
  118. }
  119. static unsigned cadet_getfreq(void)
  120. {
  121. int i;
  122. unsigned freq=0,test,fifo=0;
  123. /*
  124. * Read current tuning
  125. */
  126. fifo=cadet_gettune();
  127. /*
  128. * Convert to actual frequency
  129. */
  130. if(curtuner==0) { /* FM */
  131. test=12500;
  132. for(i=0;i<14;i++) {
  133. if((fifo&0x01)!=0) {
  134. freq+=test;
  135. }
  136. test=test<<1;
  137. fifo=fifo>>1;
  138. }
  139. freq-=10700000; /* IF frequency is 10.7 MHz */
  140. freq=(freq*16)/1000000; /* Make it 1/16 MHz */
  141. }
  142. if(curtuner==1) { /* AM */
  143. freq=((fifo&0x7fff)-2010)*16;
  144. }
  145. return freq;
  146. }
  147. static void cadet_settune(unsigned fifo)
  148. {
  149. int i;
  150. unsigned test;
  151. spin_lock(&cadet_io_lock);
  152. outb(7,io); /* Select tuner control */
  153. /*
  154. * Write the shift register
  155. */
  156. test=0;
  157. test=(fifo>>23)&0x02; /* Align data for SDO */
  158. test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
  159. outb(7,io); /* Select tuner control */
  160. outb(test,io+1); /* Initialize for write */
  161. for(i=0;i<25;i++) {
  162. test|=0x01; /* Toggle SCK High */
  163. outb(test,io+1);
  164. test&=0xfe; /* Toggle SCK Low */
  165. outb(test,io+1);
  166. fifo=fifo<<1; /* Prepare the next bit */
  167. test=0x1c|((fifo>>23)&0x02);
  168. outb(test,io+1);
  169. }
  170. spin_unlock(&cadet_io_lock);
  171. }
  172. static void cadet_setfreq(unsigned freq)
  173. {
  174. unsigned fifo;
  175. int i,j,test;
  176. int curvol;
  177. /*
  178. * Formulate a fifo command
  179. */
  180. fifo=0;
  181. if(curtuner==0) { /* FM */
  182. test=102400;
  183. freq=(freq*1000)/16; /* Make it kHz */
  184. freq+=10700; /* IF is 10700 kHz */
  185. for(i=0;i<14;i++) {
  186. fifo=fifo<<1;
  187. if(freq>=test) {
  188. fifo|=0x01;
  189. freq-=test;
  190. }
  191. test=test>>1;
  192. }
  193. }
  194. if(curtuner==1) { /* AM */
  195. fifo=(freq/16)+2010; /* Make it kHz */
  196. fifo|=0x100000; /* Select AM Band */
  197. }
  198. /*
  199. * Save current volume/mute setting
  200. */
  201. spin_lock(&cadet_io_lock);
  202. outb(7,io); /* Select tuner control */
  203. curvol=inb(io+1);
  204. spin_unlock(&cadet_io_lock);
  205. /*
  206. * Tune the card
  207. */
  208. for(j=3;j>-1;j--) {
  209. cadet_settune(fifo|(j<<16));
  210. spin_lock(&cadet_io_lock);
  211. outb(7,io); /* Select tuner control */
  212. outb(curvol,io+1);
  213. spin_unlock(&cadet_io_lock);
  214. msleep(100);
  215. cadet_gettune();
  216. if((tunestat & 0x40) == 0) { /* Tuned */
  217. sigstrength=sigtable[curtuner][j];
  218. return;
  219. }
  220. }
  221. sigstrength=0;
  222. }
  223. static int cadet_getvol(void)
  224. {
  225. int ret = 0;
  226. spin_lock(&cadet_io_lock);
  227. outb(7,io); /* Select tuner control */
  228. if((inb(io + 1) & 0x20) != 0)
  229. ret = 0xffff;
  230. spin_unlock(&cadet_io_lock);
  231. return ret;
  232. }
  233. static void cadet_setvol(int vol)
  234. {
  235. spin_lock(&cadet_io_lock);
  236. outb(7,io); /* Select tuner control */
  237. if(vol>0)
  238. outb(0x20,io+1);
  239. else
  240. outb(0x00,io+1);
  241. spin_unlock(&cadet_io_lock);
  242. }
  243. static void cadet_handler(unsigned long data)
  244. {
  245. /*
  246. * Service the RDS fifo
  247. */
  248. if(spin_trylock(&cadet_io_lock))
  249. {
  250. outb(0x3,io); /* Select RDS Decoder Control */
  251. if((inb(io+1)&0x20)!=0) {
  252. printk(KERN_CRIT "cadet: RDS fifo overflow\n");
  253. }
  254. outb(0x80,io); /* Select RDS fifo */
  255. while((inb(io)&0x80)!=0) {
  256. rdsbuf[rdsin]=inb(io+1);
  257. if(rdsin==rdsout)
  258. printk(KERN_WARNING "cadet: RDS buffer overflow\n");
  259. else
  260. rdsin++;
  261. }
  262. spin_unlock(&cadet_io_lock);
  263. }
  264. /*
  265. * Service pending read
  266. */
  267. if( rdsin!=rdsout)
  268. wake_up_interruptible(&read_queue);
  269. /*
  270. * Clean up and exit
  271. */
  272. init_timer(&readtimer);
  273. readtimer.function=cadet_handler;
  274. readtimer.data=(unsigned long)0;
  275. readtimer.expires=jiffies+(HZ/20);
  276. add_timer(&readtimer);
  277. }
  278. static ssize_t cadet_read(struct file *file, char __user *data,
  279. size_t count, loff_t *ppos)
  280. {
  281. int i=0;
  282. unsigned char readbuf[RDS_BUFFER];
  283. if(rdsstat==0) {
  284. spin_lock(&cadet_io_lock);
  285. rdsstat=1;
  286. outb(0x80,io); /* Select RDS fifo */
  287. spin_unlock(&cadet_io_lock);
  288. init_timer(&readtimer);
  289. readtimer.function=cadet_handler;
  290. readtimer.data=(unsigned long)0;
  291. readtimer.expires=jiffies+(HZ/20);
  292. add_timer(&readtimer);
  293. }
  294. if(rdsin==rdsout) {
  295. if (file->f_flags & O_NONBLOCK)
  296. return -EWOULDBLOCK;
  297. interruptible_sleep_on(&read_queue);
  298. }
  299. while( i<count && rdsin!=rdsout)
  300. readbuf[i++]=rdsbuf[rdsout++];
  301. if (copy_to_user(data,readbuf,i))
  302. return -EFAULT;
  303. return i;
  304. }
  305. static int cadet_do_ioctl(struct inode *inode, struct file *file,
  306. unsigned int cmd, void *arg)
  307. {
  308. switch(cmd)
  309. {
  310. case VIDIOCGCAP:
  311. {
  312. struct video_capability *v = arg;
  313. memset(v,0,sizeof(*v));
  314. v->type=VID_TYPE_TUNER;
  315. v->channels=2;
  316. v->audios=1;
  317. strcpy(v->name, "ADS Cadet");
  318. return 0;
  319. }
  320. case VIDIOCGTUNER:
  321. {
  322. struct video_tuner *v = arg;
  323. if((v->tuner<0)||(v->tuner>1)) {
  324. return -EINVAL;
  325. }
  326. switch(v->tuner) {
  327. case 0:
  328. strcpy(v->name,"FM");
  329. v->rangelow=1400; /* 87.5 MHz */
  330. v->rangehigh=1728; /* 108.0 MHz */
  331. v->flags=0;
  332. v->mode=0;
  333. v->mode|=VIDEO_MODE_AUTO;
  334. v->signal=sigstrength;
  335. if(cadet_getstereo()==1) {
  336. v->flags|=VIDEO_TUNER_STEREO_ON;
  337. }
  338. v->flags|=cadet_getrds();
  339. break;
  340. case 1:
  341. strcpy(v->name,"AM");
  342. v->rangelow=8320; /* 520 kHz */
  343. v->rangehigh=26400; /* 1650 kHz */
  344. v->flags=0;
  345. v->flags|=VIDEO_TUNER_LOW;
  346. v->mode=0;
  347. v->mode|=VIDEO_MODE_AUTO;
  348. v->signal=sigstrength;
  349. break;
  350. }
  351. return 0;
  352. }
  353. case VIDIOCSTUNER:
  354. {
  355. struct video_tuner *v = arg;
  356. if((v->tuner<0)||(v->tuner>1)) {
  357. return -EINVAL;
  358. }
  359. curtuner=v->tuner;
  360. return 0;
  361. }
  362. case VIDIOCGFREQ:
  363. {
  364. unsigned long *freq = arg;
  365. *freq = cadet_getfreq();
  366. return 0;
  367. }
  368. case VIDIOCSFREQ:
  369. {
  370. unsigned long *freq = arg;
  371. if((curtuner==0)&&((*freq<1400)||(*freq>1728))) {
  372. return -EINVAL;
  373. }
  374. if((curtuner==1)&&((*freq<8320)||(*freq>26400))) {
  375. return -EINVAL;
  376. }
  377. cadet_setfreq(*freq);
  378. return 0;
  379. }
  380. case VIDIOCGAUDIO:
  381. {
  382. struct video_audio *v = arg;
  383. memset(v,0, sizeof(*v));
  384. v->flags=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
  385. if(cadet_getstereo()==0) {
  386. v->mode=VIDEO_SOUND_MONO;
  387. } else {
  388. v->mode=VIDEO_SOUND_STEREO;
  389. }
  390. v->volume=cadet_getvol();
  391. v->step=0xffff;
  392. strcpy(v->name, "Radio");
  393. return 0;
  394. }
  395. case VIDIOCSAUDIO:
  396. {
  397. struct video_audio *v = arg;
  398. if(v->audio)
  399. return -EINVAL;
  400. cadet_setvol(v->volume);
  401. if(v->flags&VIDEO_AUDIO_MUTE)
  402. cadet_setvol(0);
  403. else
  404. cadet_setvol(0xffff);
  405. return 0;
  406. }
  407. default:
  408. return -ENOIOCTLCMD;
  409. }
  410. }
  411. static int cadet_ioctl(struct inode *inode, struct file *file,
  412. unsigned int cmd, unsigned long arg)
  413. {
  414. return video_usercopy(inode, file, cmd, arg, cadet_do_ioctl);
  415. }
  416. static int cadet_open(struct inode *inode, struct file *file)
  417. {
  418. if(users)
  419. return -EBUSY;
  420. users++;
  421. init_waitqueue_head(&read_queue);
  422. return 0;
  423. }
  424. static int cadet_release(struct inode *inode, struct file *file)
  425. {
  426. del_timer_sync(&readtimer);
  427. rdsstat=0;
  428. users--;
  429. return 0;
  430. }
  431. static struct file_operations cadet_fops = {
  432. .owner = THIS_MODULE,
  433. .open = cadet_open,
  434. .release = cadet_release,
  435. .read = cadet_read,
  436. .ioctl = cadet_ioctl,
  437. .compat_ioctl = v4l_compat_ioctl32,
  438. .llseek = no_llseek,
  439. };
  440. static struct video_device cadet_radio=
  441. {
  442. .owner = THIS_MODULE,
  443. .name = "Cadet radio",
  444. .type = VID_TYPE_TUNER,
  445. .hardware = VID_HARDWARE_CADET,
  446. .fops = &cadet_fops,
  447. };
  448. static struct pnp_device_id cadet_pnp_devices[] = {
  449. /* ADS Cadet AM/FM Radio Card */
  450. {.id = "MSM0c24", .driver_data = 0},
  451. {.id = ""}
  452. };
  453. MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
  454. static int cadet_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
  455. {
  456. if (!dev)
  457. return -ENODEV;
  458. /* only support one device */
  459. if (io > 0)
  460. return -EBUSY;
  461. if (!pnp_port_valid(dev, 0)) {
  462. return -ENODEV;
  463. }
  464. io = pnp_port_start(dev, 0);
  465. printk ("radio-cadet: PnP reports device at %#x\n", io);
  466. return io;
  467. }
  468. static struct pnp_driver cadet_pnp_driver = {
  469. .name = "radio-cadet",
  470. .id_table = cadet_pnp_devices,
  471. .probe = cadet_pnp_probe,
  472. .remove = NULL,
  473. };
  474. static int cadet_probe(void)
  475. {
  476. static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
  477. int i;
  478. for(i=0;i<8;i++) {
  479. io=iovals[i];
  480. if (request_region(io, 2, "cadet-probe")) {
  481. cadet_setfreq(1410);
  482. if(cadet_getfreq()==1410) {
  483. release_region(io, 2);
  484. return io;
  485. }
  486. release_region(io, 2);
  487. }
  488. }
  489. return -1;
  490. }
  491. /*
  492. * io should only be set if the user has used something like
  493. * isapnp (the userspace program) to initialize this card for us
  494. */
  495. static int __init cadet_init(void)
  496. {
  497. spin_lock_init(&cadet_io_lock);
  498. /*
  499. * If a probe was requested then probe ISAPnP first (safest)
  500. */
  501. if (io < 0)
  502. pnp_register_driver(&cadet_pnp_driver);
  503. /*
  504. * If that fails then probe unsafely if probe is requested
  505. */
  506. if(io < 0)
  507. io = cadet_probe ();
  508. /*
  509. * Else we bail out
  510. */
  511. if(io < 0) {
  512. #ifdef MODULE
  513. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  514. #endif
  515. goto fail;
  516. }
  517. if (!request_region(io,2,"cadet"))
  518. goto fail;
  519. if(video_register_device(&cadet_radio,VFL_TYPE_RADIO,radio_nr)==-1) {
  520. release_region(io,2);
  521. goto fail;
  522. }
  523. printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io);
  524. return 0;
  525. fail:
  526. pnp_unregister_driver(&cadet_pnp_driver);
  527. return -1;
  528. }
  529. MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  530. MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
  531. MODULE_LICENSE("GPL");
  532. module_param(io, int, 0);
  533. MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
  534. module_param(radio_nr, int, 0);
  535. static void __exit cadet_cleanup_module(void)
  536. {
  537. video_unregister_device(&cadet_radio);
  538. release_region(io,2);
  539. pnp_unregister_driver(&cadet_pnp_driver);
  540. }
  541. module_init(cadet_init);
  542. module_exit(cadet_cleanup_module);