radio-cadet.c 13 KB

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