radio-miropcm20.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <media/v4l2-ctrls.h>
  19. #include <media/v4l2-fh.h>
  20. #include <media/v4l2-event.h>
  21. #include <sound/aci.h>
  22. static int radio_nr = -1;
  23. module_param(radio_nr, int, 0);
  24. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  25. struct pcm20 {
  26. struct v4l2_device v4l2_dev;
  27. struct video_device vdev;
  28. struct v4l2_ctrl_handler ctrl_handler;
  29. unsigned long freq;
  30. u32 audmode;
  31. struct snd_miro_aci *aci;
  32. struct mutex lock;
  33. };
  34. static struct pcm20 pcm20_card = {
  35. .freq = 87 * 16000,
  36. .audmode = V4L2_TUNER_MODE_STEREO,
  37. };
  38. static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
  39. {
  40. unsigned char freql;
  41. unsigned char freqh;
  42. struct snd_miro_aci *aci = dev->aci;
  43. freq /= 160;
  44. if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
  45. freq /= 10; /* I don't know exactly which version
  46. * needs this hack */
  47. freql = freq & 0xff;
  48. freqh = freq >> 8;
  49. return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
  50. }
  51. static const struct v4l2_file_operations pcm20_fops = {
  52. .owner = THIS_MODULE,
  53. .open = v4l2_fh_open,
  54. .poll = v4l2_ctrl_poll,
  55. .release = v4l2_fh_release,
  56. .unlocked_ioctl = video_ioctl2,
  57. };
  58. static int vidioc_querycap(struct file *file, void *priv,
  59. struct v4l2_capability *v)
  60. {
  61. struct pcm20 *dev = video_drvdata(file);
  62. strlcpy(v->driver, "Miro PCM20", sizeof(v->driver));
  63. strlcpy(v->card, "Miro PCM20", sizeof(v->card));
  64. snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name);
  65. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  66. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  67. return 0;
  68. }
  69. static int vidioc_g_tuner(struct file *file, void *priv,
  70. struct v4l2_tuner *v)
  71. {
  72. struct pcm20 *dev = video_drvdata(file);
  73. int res;
  74. if (v->index)
  75. return -EINVAL;
  76. strlcpy(v->name, "FM", sizeof(v->name));
  77. v->type = V4L2_TUNER_RADIO;
  78. v->rangelow = 87*16000;
  79. v->rangehigh = 108*16000;
  80. res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1);
  81. v->signal = (res & 0x80) ? 0 : 0xffff;
  82. /* Note: stereo detection does not work if the audio is muted,
  83. it will default to mono in that case. */
  84. res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTEREO, -1, -1);
  85. v->rxsubchans = (res & 0x40) ? V4L2_TUNER_SUB_MONO :
  86. V4L2_TUNER_SUB_STEREO;
  87. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  88. v->audmode = dev->audmode;
  89. return 0;
  90. }
  91. static int vidioc_s_tuner(struct file *file, void *priv,
  92. struct v4l2_tuner *v)
  93. {
  94. struct pcm20 *dev = video_drvdata(file);
  95. if (v->index)
  96. return -EINVAL;
  97. if (v->audmode > V4L2_TUNER_MODE_STEREO)
  98. v->audmode = V4L2_TUNER_MODE_STEREO;
  99. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  100. v->audmode == V4L2_TUNER_MODE_MONO, -1);
  101. return 0;
  102. }
  103. static int vidioc_g_frequency(struct file *file, void *priv,
  104. struct v4l2_frequency *f)
  105. {
  106. struct pcm20 *dev = video_drvdata(file);
  107. if (f->tuner != 0)
  108. return -EINVAL;
  109. f->type = V4L2_TUNER_RADIO;
  110. f->frequency = dev->freq;
  111. return 0;
  112. }
  113. static int vidioc_s_frequency(struct file *file, void *priv,
  114. struct v4l2_frequency *f)
  115. {
  116. struct pcm20 *dev = video_drvdata(file);
  117. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  118. return -EINVAL;
  119. dev->freq = clamp(f->frequency, 87 * 16000U, 108 * 16000U);
  120. pcm20_setfreq(dev, dev->freq);
  121. return 0;
  122. }
  123. static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl)
  124. {
  125. struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler);
  126. switch (ctrl->id) {
  127. case V4L2_CID_AUDIO_MUTE:
  128. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1);
  129. return 0;
  130. }
  131. return -EINVAL;
  132. }
  133. static const struct v4l2_ioctl_ops pcm20_ioctl_ops = {
  134. .vidioc_querycap = vidioc_querycap,
  135. .vidioc_g_tuner = vidioc_g_tuner,
  136. .vidioc_s_tuner = vidioc_s_tuner,
  137. .vidioc_g_frequency = vidioc_g_frequency,
  138. .vidioc_s_frequency = vidioc_s_frequency,
  139. .vidioc_log_status = v4l2_ctrl_log_status,
  140. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  141. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  142. };
  143. static const struct v4l2_ctrl_ops pcm20_ctrl_ops = {
  144. .s_ctrl = pcm20_s_ctrl,
  145. };
  146. static int __init pcm20_init(void)
  147. {
  148. struct pcm20 *dev = &pcm20_card;
  149. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  150. struct v4l2_ctrl_handler *hdl;
  151. int res;
  152. dev->aci = snd_aci_get_aci();
  153. if (dev->aci == NULL) {
  154. v4l2_err(v4l2_dev,
  155. "you must load the snd-miro driver first!\n");
  156. return -ENODEV;
  157. }
  158. strlcpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name));
  159. mutex_init(&dev->lock);
  160. res = v4l2_device_register(NULL, v4l2_dev);
  161. if (res < 0) {
  162. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  163. return -EINVAL;
  164. }
  165. hdl = &dev->ctrl_handler;
  166. v4l2_ctrl_handler_init(hdl, 1);
  167. v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops,
  168. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  169. v4l2_dev->ctrl_handler = hdl;
  170. if (hdl->error) {
  171. res = hdl->error;
  172. v4l2_err(v4l2_dev, "Could not register control\n");
  173. goto err_hdl;
  174. }
  175. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  176. dev->vdev.v4l2_dev = v4l2_dev;
  177. dev->vdev.fops = &pcm20_fops;
  178. dev->vdev.ioctl_ops = &pcm20_ioctl_ops;
  179. dev->vdev.release = video_device_release_empty;
  180. dev->vdev.lock = &dev->lock;
  181. set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
  182. video_set_drvdata(&dev->vdev, dev);
  183. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  184. dev->audmode == V4L2_TUNER_MODE_MONO, -1);
  185. pcm20_setfreq(dev, dev->freq);
  186. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
  187. goto err_hdl;
  188. v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
  189. return 0;
  190. err_hdl:
  191. v4l2_ctrl_handler_free(hdl);
  192. v4l2_device_unregister(v4l2_dev);
  193. return -EINVAL;
  194. }
  195. MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
  196. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  197. MODULE_LICENSE("GPL");
  198. static void __exit pcm20_cleanup(void)
  199. {
  200. struct pcm20 *dev = &pcm20_card;
  201. video_unregister_device(&dev->vdev);
  202. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1);
  203. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  204. v4l2_device_unregister(&dev->v4l2_dev);
  205. }
  206. module_init(pcm20_init);
  207. module_exit(pcm20_cleanup);