radio-cadet.c 15 KB

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