miropcm20-radio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Miro PCM20 radio driver for Linux radio support
  2. * (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
  3. * Thanks to Norberto Pellici for the ACI device interface specification
  4. * The API part is based on the radiotrack driver by M. Kirkwood
  5. * This driver relies on the aci mixer (drivers/sound/aci.c)
  6. * Look there for further info...
  7. */
  8. /* Revision history:
  9. *
  10. * 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
  11. * 2000-09-05 Robert Siemer <Robert.Siemer@gmx.de>
  12. * removed unfinished volume control (maybe adding it later again)
  13. * use OSS-mixer; added stereo control
  14. */
  15. /* What ever you think about the ACI, version 0x07 is not very well!
  16. * I can't get frequency, 'tuner status', 'tuner flags' or mute/mono
  17. * conditions... Robert
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/videodev.h>
  22. #include <media/v4l2-common.h>
  23. #include "oss/aci.h"
  24. #include "miropcm20-rds-core.h"
  25. static int radio_nr = -1;
  26. module_param(radio_nr, int, 0);
  27. struct pcm20_device {
  28. unsigned long freq;
  29. int muted;
  30. int stereo;
  31. };
  32. static int pcm20_mute(struct pcm20_device *dev, unsigned char mute)
  33. {
  34. dev->muted = mute;
  35. return aci_write_cmd(ACI_SET_TUNERMUTE, mute);
  36. }
  37. static int pcm20_stereo(struct pcm20_device *dev, unsigned char stereo)
  38. {
  39. dev->stereo = stereo;
  40. return aci_write_cmd(ACI_SET_TUNERMONO, !stereo);
  41. }
  42. static int pcm20_setfreq(struct pcm20_device *dev, unsigned long freq)
  43. {
  44. unsigned char freql;
  45. unsigned char freqh;
  46. dev->freq=freq;
  47. freq /= 160;
  48. if (!(aci_version==0x07 || aci_version>=0xb0))
  49. freq /= 10; /* I don't know exactly which version
  50. * needs this hack */
  51. freql = freq & 0xff;
  52. freqh = freq >> 8;
  53. aci_rds_cmd(RDS_RESET, NULL, 0);
  54. pcm20_stereo(dev, 1);
  55. return aci_rw_cmd(ACI_WRITE_TUNE, freql, freqh);
  56. }
  57. static int pcm20_getflags(struct pcm20_device *dev, __u32 *flags, __u16 *signal)
  58. {
  59. /* okay, check for signal, stereo and rds here... */
  60. int i;
  61. unsigned char buf;
  62. if ((i=aci_rw_cmd(ACI_READ_TUNERSTATION, -1, -1))<0)
  63. return i;
  64. pr_debug("check_sig: 0x%x\n", i);
  65. if (i & 0x80) {
  66. /* no signal from tuner */
  67. *flags=0;
  68. *signal=0;
  69. return 0;
  70. } else
  71. *signal=0xffff;
  72. if ((i=aci_rw_cmd(ACI_READ_TUNERSTEREO, -1, -1))<0)
  73. return i;
  74. if (i & 0x40) {
  75. *flags=0;
  76. } else {
  77. /* stereo */
  78. *flags=VIDEO_TUNER_STEREO_ON;
  79. /* I can't see stereo, when forced to mono */
  80. dev->stereo=1;
  81. }
  82. if ((i=aci_rds_cmd(RDS_STATUS, &buf, 1))<0)
  83. return i;
  84. if (buf & 1)
  85. /* RDS available */
  86. *flags|=VIDEO_TUNER_RDS_ON;
  87. else
  88. return 0;
  89. if ((i=aci_rds_cmd(RDS_RXVALUE, &buf, 1))<0)
  90. return i;
  91. pr_debug("rds-signal: %d\n", buf);
  92. if (buf > 15) {
  93. printk("miropcm20-radio: RX strengths unexpected high...\n");
  94. buf=15;
  95. }
  96. /* refine signal */
  97. if ((*signal=SCALE(15, 0xffff, buf))==0)
  98. *signal = 1;
  99. return 0;
  100. }
  101. static int pcm20_do_ioctl(struct inode *inode, struct file *file,
  102. unsigned int cmd, void *arg)
  103. {
  104. struct video_device *dev = video_devdata(file);
  105. struct pcm20_device *pcm20 = dev->priv;
  106. int i;
  107. switch(cmd)
  108. {
  109. case VIDIOCGCAP:
  110. {
  111. struct video_capability *v = arg;
  112. memset(v,0,sizeof(*v));
  113. v->type=VID_TYPE_TUNER;
  114. strcpy(v->name, "Miro PCM20");
  115. v->channels=1;
  116. v->audios=1;
  117. return 0;
  118. }
  119. case VIDIOCGTUNER:
  120. {
  121. struct video_tuner *v = arg;
  122. if(v->tuner) /* Only 1 tuner */
  123. return -EINVAL;
  124. v->rangelow=87*16000;
  125. v->rangehigh=108*16000;
  126. pcm20_getflags(pcm20, &v->flags, &v->signal);
  127. v->flags|=VIDEO_TUNER_LOW;
  128. v->mode=VIDEO_MODE_AUTO;
  129. strcpy(v->name, "FM");
  130. return 0;
  131. }
  132. case VIDIOCSTUNER:
  133. {
  134. struct video_tuner *v = arg;
  135. if(v->tuner!=0)
  136. return -EINVAL;
  137. /* Only 1 tuner so no setting needed ! */
  138. return 0;
  139. }
  140. case VIDIOCGFREQ:
  141. {
  142. unsigned long *freq = arg;
  143. *freq = pcm20->freq;
  144. return 0;
  145. }
  146. case VIDIOCSFREQ:
  147. {
  148. unsigned long *freq = arg;
  149. pcm20->freq = *freq;
  150. i=pcm20_setfreq(pcm20, pcm20->freq);
  151. pr_debug("First view (setfreq): 0x%x\n", i);
  152. return i;
  153. }
  154. case VIDIOCGAUDIO:
  155. {
  156. struct video_audio *v = arg;
  157. memset(v,0, sizeof(*v));
  158. v->flags=VIDEO_AUDIO_MUTABLE;
  159. if (pcm20->muted)
  160. v->flags|=VIDEO_AUDIO_MUTE;
  161. v->mode=VIDEO_SOUND_STEREO;
  162. if (pcm20->stereo)
  163. v->mode|=VIDEO_SOUND_MONO;
  164. /* v->step=2048; */
  165. strcpy(v->name, "Radio");
  166. return 0;
  167. }
  168. case VIDIOCSAUDIO:
  169. {
  170. struct video_audio *v = arg;
  171. if(v->audio)
  172. return -EINVAL;
  173. pcm20_mute(pcm20, !!(v->flags&VIDEO_AUDIO_MUTE));
  174. if(v->flags&VIDEO_SOUND_MONO)
  175. pcm20_stereo(pcm20, 0);
  176. if(v->flags&VIDEO_SOUND_STEREO)
  177. pcm20_stereo(pcm20, 1);
  178. return 0;
  179. }
  180. default:
  181. return -ENOIOCTLCMD;
  182. }
  183. }
  184. static int pcm20_ioctl(struct inode *inode, struct file *file,
  185. unsigned int cmd, unsigned long arg)
  186. {
  187. return video_usercopy(inode, file, cmd, arg, pcm20_do_ioctl);
  188. }
  189. static struct pcm20_device pcm20_unit = {
  190. .freq = 87*16000,
  191. .muted = 1,
  192. };
  193. static struct file_operations pcm20_fops = {
  194. .owner = THIS_MODULE,
  195. .open = video_exclusive_open,
  196. .release = video_exclusive_release,
  197. .ioctl = pcm20_ioctl,
  198. .compat_ioctl = v4l_compat_ioctl32,
  199. .llseek = no_llseek,
  200. };
  201. static struct video_device pcm20_radio = {
  202. .owner = THIS_MODULE,
  203. .name = "Miro PCM 20 radio",
  204. .type = VID_TYPE_TUNER,
  205. .hardware = VID_HARDWARE_RTRACK,
  206. .fops = &pcm20_fops,
  207. .priv = &pcm20_unit
  208. };
  209. static int __init pcm20_init(void)
  210. {
  211. if(video_register_device(&pcm20_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  212. goto video_register_device;
  213. if(attach_aci_rds()<0)
  214. goto attach_aci_rds;
  215. printk(KERN_INFO "Miro PCM20 radio card driver.\n");
  216. return 0;
  217. attach_aci_rds:
  218. video_unregister_device(&pcm20_radio);
  219. video_register_device:
  220. return -EINVAL;
  221. }
  222. MODULE_AUTHOR("Ruurd Reitsma");
  223. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  224. MODULE_LICENSE("GPL");
  225. static void __exit pcm20_cleanup(void)
  226. {
  227. unload_aci_rds();
  228. video_unregister_device(&pcm20_radio);
  229. }
  230. module_init(pcm20_init);
  231. module_exit(pcm20_cleanup);