radio-miropcm20.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. const 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. dev->audmode = V4L2_TUNER_MODE_STEREO;
  99. else
  100. dev->audmode = v->audmode;
  101. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  102. dev->audmode == V4L2_TUNER_MODE_MONO, -1);
  103. return 0;
  104. }
  105. static int vidioc_g_frequency(struct file *file, void *priv,
  106. struct v4l2_frequency *f)
  107. {
  108. struct pcm20 *dev = video_drvdata(file);
  109. if (f->tuner != 0)
  110. return -EINVAL;
  111. f->type = V4L2_TUNER_RADIO;
  112. f->frequency = dev->freq;
  113. return 0;
  114. }
  115. static int vidioc_s_frequency(struct file *file, void *priv,
  116. const struct v4l2_frequency *f)
  117. {
  118. struct pcm20 *dev = video_drvdata(file);
  119. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  120. return -EINVAL;
  121. dev->freq = clamp_t(u32, f->frequency, 87 * 16000U, 108 * 16000U);
  122. pcm20_setfreq(dev, dev->freq);
  123. return 0;
  124. }
  125. static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl)
  126. {
  127. struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler);
  128. switch (ctrl->id) {
  129. case V4L2_CID_AUDIO_MUTE:
  130. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1);
  131. return 0;
  132. }
  133. return -EINVAL;
  134. }
  135. static const struct v4l2_ioctl_ops pcm20_ioctl_ops = {
  136. .vidioc_querycap = vidioc_querycap,
  137. .vidioc_g_tuner = vidioc_g_tuner,
  138. .vidioc_s_tuner = vidioc_s_tuner,
  139. .vidioc_g_frequency = vidioc_g_frequency,
  140. .vidioc_s_frequency = vidioc_s_frequency,
  141. .vidioc_log_status = v4l2_ctrl_log_status,
  142. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  143. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  144. };
  145. static const struct v4l2_ctrl_ops pcm20_ctrl_ops = {
  146. .s_ctrl = pcm20_s_ctrl,
  147. };
  148. static int __init pcm20_init(void)
  149. {
  150. struct pcm20 *dev = &pcm20_card;
  151. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  152. struct v4l2_ctrl_handler *hdl;
  153. int res;
  154. dev->aci = snd_aci_get_aci();
  155. if (dev->aci == NULL) {
  156. v4l2_err(v4l2_dev,
  157. "you must load the snd-miro driver first!\n");
  158. return -ENODEV;
  159. }
  160. strlcpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name));
  161. mutex_init(&dev->lock);
  162. res = v4l2_device_register(NULL, v4l2_dev);
  163. if (res < 0) {
  164. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  165. return -EINVAL;
  166. }
  167. hdl = &dev->ctrl_handler;
  168. v4l2_ctrl_handler_init(hdl, 1);
  169. v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops,
  170. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  171. v4l2_dev->ctrl_handler = hdl;
  172. if (hdl->error) {
  173. res = hdl->error;
  174. v4l2_err(v4l2_dev, "Could not register control\n");
  175. goto err_hdl;
  176. }
  177. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  178. dev->vdev.v4l2_dev = v4l2_dev;
  179. dev->vdev.fops = &pcm20_fops;
  180. dev->vdev.ioctl_ops = &pcm20_ioctl_ops;
  181. dev->vdev.release = video_device_release_empty;
  182. dev->vdev.lock = &dev->lock;
  183. set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
  184. video_set_drvdata(&dev->vdev, dev);
  185. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  186. dev->audmode == V4L2_TUNER_MODE_MONO, -1);
  187. pcm20_setfreq(dev, dev->freq);
  188. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
  189. goto err_hdl;
  190. v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
  191. return 0;
  192. err_hdl:
  193. v4l2_ctrl_handler_free(hdl);
  194. v4l2_device_unregister(v4l2_dev);
  195. return -EINVAL;
  196. }
  197. MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
  198. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  199. MODULE_LICENSE("GPL");
  200. static void __exit pcm20_cleanup(void)
  201. {
  202. struct pcm20 *dev = &pcm20_card;
  203. video_unregister_device(&dev->vdev);
  204. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1);
  205. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  206. v4l2_device_unregister(&dev->v4l2_dev);
  207. }
  208. module_init(pcm20_init);
  209. module_exit(pcm20_cleanup);