radio-miropcm20.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 provided by the snd-miro
  6. * ALSA driver.
  7. * Look there for further info...
  8. */
  9. /* What ever you think about the ACI, version 0x07 is not very well!
  10. * I can't get frequency, 'tuner status', 'tuner flags' or mute/mono
  11. * conditions... Robert
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/videodev2.h>
  16. #include <media/v4l2-device.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include <sound/aci.h>
  19. static int radio_nr = -1;
  20. module_param(radio_nr, int, 0);
  21. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  22. static bool mono;
  23. module_param(mono, bool, 0);
  24. MODULE_PARM_DESC(mono, "Force tuner into mono mode.");
  25. struct pcm20 {
  26. struct v4l2_device v4l2_dev;
  27. struct video_device vdev;
  28. unsigned long freq;
  29. int muted;
  30. struct snd_miro_aci *aci;
  31. struct mutex lock;
  32. };
  33. static struct pcm20 pcm20_card = {
  34. .freq = 87*16000,
  35. .muted = 1,
  36. };
  37. static int pcm20_mute(struct pcm20 *dev, unsigned char mute)
  38. {
  39. dev->muted = mute;
  40. return snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, mute, -1);
  41. }
  42. static int pcm20_stereo(struct pcm20 *dev, unsigned char stereo)
  43. {
  44. return snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, !stereo, -1);
  45. }
  46. static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
  47. {
  48. unsigned char freql;
  49. unsigned char freqh;
  50. struct snd_miro_aci *aci = dev->aci;
  51. dev->freq = freq;
  52. freq /= 160;
  53. if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
  54. freq /= 10; /* I don't know exactly which version
  55. * needs this hack */
  56. freql = freq & 0xff;
  57. freqh = freq >> 8;
  58. pcm20_stereo(dev, !mono);
  59. return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
  60. }
  61. static const struct v4l2_file_operations pcm20_fops = {
  62. .owner = THIS_MODULE,
  63. .unlocked_ioctl = video_ioctl2,
  64. };
  65. static int vidioc_querycap(struct file *file, void *priv,
  66. struct v4l2_capability *v)
  67. {
  68. strlcpy(v->driver, "Miro PCM20", sizeof(v->driver));
  69. strlcpy(v->card, "Miro PCM20", sizeof(v->card));
  70. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  71. v->version = 0x1;
  72. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  73. return 0;
  74. }
  75. static int vidioc_g_tuner(struct file *file, void *priv,
  76. struct v4l2_tuner *v)
  77. {
  78. if (v->index) /* Only 1 tuner */
  79. return -EINVAL;
  80. strlcpy(v->name, "FM", sizeof(v->name));
  81. v->type = V4L2_TUNER_RADIO;
  82. v->rangelow = 87*16000;
  83. v->rangehigh = 108*16000;
  84. v->signal = 0xffff;
  85. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  86. v->capability = V4L2_TUNER_CAP_LOW;
  87. v->audmode = V4L2_TUNER_MODE_MONO;
  88. return 0;
  89. }
  90. static int vidioc_s_tuner(struct file *file, void *priv,
  91. struct v4l2_tuner *v)
  92. {
  93. return v->index ? -EINVAL : 0;
  94. }
  95. static int vidioc_g_frequency(struct file *file, void *priv,
  96. struct v4l2_frequency *f)
  97. {
  98. struct pcm20 *dev = video_drvdata(file);
  99. if (f->tuner != 0)
  100. return -EINVAL;
  101. f->type = V4L2_TUNER_RADIO;
  102. f->frequency = dev->freq;
  103. return 0;
  104. }
  105. static int vidioc_s_frequency(struct file *file, void *priv,
  106. struct v4l2_frequency *f)
  107. {
  108. struct pcm20 *dev = video_drvdata(file);
  109. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  110. return -EINVAL;
  111. dev->freq = f->frequency;
  112. pcm20_setfreq(dev, f->frequency);
  113. return 0;
  114. }
  115. static int vidioc_queryctrl(struct file *file, void *priv,
  116. struct v4l2_queryctrl *qc)
  117. {
  118. switch (qc->id) {
  119. case V4L2_CID_AUDIO_MUTE:
  120. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  121. }
  122. return -EINVAL;
  123. }
  124. static int vidioc_g_ctrl(struct file *file, void *priv,
  125. struct v4l2_control *ctrl)
  126. {
  127. struct pcm20 *dev = video_drvdata(file);
  128. switch (ctrl->id) {
  129. case V4L2_CID_AUDIO_MUTE:
  130. ctrl->value = dev->muted;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. return 0;
  136. }
  137. static int vidioc_s_ctrl(struct file *file, void *priv,
  138. struct v4l2_control *ctrl)
  139. {
  140. struct pcm20 *dev = video_drvdata(file);
  141. switch (ctrl->id) {
  142. case V4L2_CID_AUDIO_MUTE:
  143. pcm20_mute(dev, ctrl->value);
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. return 0;
  149. }
  150. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  151. {
  152. *i = 0;
  153. return 0;
  154. }
  155. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  156. {
  157. return i ? -EINVAL : 0;
  158. }
  159. static int vidioc_g_audio(struct file *file, void *priv,
  160. struct v4l2_audio *a)
  161. {
  162. a->index = 0;
  163. strlcpy(a->name, "Radio", sizeof(a->name));
  164. a->capability = V4L2_AUDCAP_STEREO;
  165. return 0;
  166. }
  167. static int vidioc_s_audio(struct file *file, void *priv,
  168. struct v4l2_audio *a)
  169. {
  170. return a->index ? -EINVAL : 0;
  171. }
  172. static const struct v4l2_ioctl_ops pcm20_ioctl_ops = {
  173. .vidioc_querycap = vidioc_querycap,
  174. .vidioc_g_tuner = vidioc_g_tuner,
  175. .vidioc_s_tuner = vidioc_s_tuner,
  176. .vidioc_g_frequency = vidioc_g_frequency,
  177. .vidioc_s_frequency = vidioc_s_frequency,
  178. .vidioc_queryctrl = vidioc_queryctrl,
  179. .vidioc_g_ctrl = vidioc_g_ctrl,
  180. .vidioc_s_ctrl = vidioc_s_ctrl,
  181. .vidioc_g_audio = vidioc_g_audio,
  182. .vidioc_s_audio = vidioc_s_audio,
  183. .vidioc_g_input = vidioc_g_input,
  184. .vidioc_s_input = vidioc_s_input,
  185. };
  186. static int __init pcm20_init(void)
  187. {
  188. struct pcm20 *dev = &pcm20_card;
  189. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  190. int res;
  191. dev->aci = snd_aci_get_aci();
  192. if (dev->aci == NULL) {
  193. v4l2_err(v4l2_dev,
  194. "you must load the snd-miro driver first!\n");
  195. return -ENODEV;
  196. }
  197. strlcpy(v4l2_dev->name, "miropcm20", sizeof(v4l2_dev->name));
  198. mutex_init(&dev->lock);
  199. res = v4l2_device_register(NULL, v4l2_dev);
  200. if (res < 0) {
  201. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  202. return -EINVAL;
  203. }
  204. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  205. dev->vdev.v4l2_dev = v4l2_dev;
  206. dev->vdev.fops = &pcm20_fops;
  207. dev->vdev.ioctl_ops = &pcm20_ioctl_ops;
  208. dev->vdev.release = video_device_release_empty;
  209. dev->vdev.lock = &dev->lock;
  210. video_set_drvdata(&dev->vdev, dev);
  211. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
  212. goto fail;
  213. v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
  214. return 0;
  215. fail:
  216. v4l2_device_unregister(v4l2_dev);
  217. return -EINVAL;
  218. }
  219. MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
  220. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  221. MODULE_LICENSE("GPL");
  222. static void __exit pcm20_cleanup(void)
  223. {
  224. struct pcm20 *dev = &pcm20_card;
  225. video_unregister_device(&dev->vdev);
  226. v4l2_device_unregister(&dev->v4l2_dev);
  227. }
  228. module_init(pcm20_init);
  229. module_exit(pcm20_cleanup);