radio-miropcm20.c 6.2 KB

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