radio-miropcm20.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. static bool mono;
  26. module_param(mono, bool, 0);
  27. MODULE_PARM_DESC(mono, "Force tuner into mono mode.");
  28. struct pcm20 {
  29. struct v4l2_device v4l2_dev;
  30. struct video_device vdev;
  31. struct v4l2_ctrl_handler ctrl_handler;
  32. unsigned long freq;
  33. int muted;
  34. struct snd_miro_aci *aci;
  35. struct mutex lock;
  36. };
  37. static struct pcm20 pcm20_card = {
  38. .freq = 87*16000,
  39. .muted = 1,
  40. };
  41. static int pcm20_mute(struct pcm20 *dev, unsigned char mute)
  42. {
  43. dev->muted = mute;
  44. return snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, mute, -1);
  45. }
  46. static int pcm20_stereo(struct pcm20 *dev, unsigned char stereo)
  47. {
  48. return snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, !stereo, -1);
  49. }
  50. static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
  51. {
  52. unsigned char freql;
  53. unsigned char freqh;
  54. struct snd_miro_aci *aci = dev->aci;
  55. dev->freq = freq;
  56. freq /= 160;
  57. if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
  58. freq /= 10; /* I don't know exactly which version
  59. * needs this hack */
  60. freql = freq & 0xff;
  61. freqh = freq >> 8;
  62. pcm20_stereo(dev, !mono);
  63. return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
  64. }
  65. static const struct v4l2_file_operations pcm20_fops = {
  66. .owner = THIS_MODULE,
  67. .open = v4l2_fh_open,
  68. .poll = v4l2_ctrl_poll,
  69. .release = v4l2_fh_release,
  70. .unlocked_ioctl = video_ioctl2,
  71. };
  72. static int vidioc_querycap(struct file *file, void *priv,
  73. struct v4l2_capability *v)
  74. {
  75. struct pcm20 *dev = video_drvdata(file);
  76. strlcpy(v->driver, "Miro PCM20", sizeof(v->driver));
  77. strlcpy(v->card, "Miro PCM20", sizeof(v->card));
  78. snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name);
  79. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  80. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  81. return 0;
  82. }
  83. static int vidioc_g_tuner(struct file *file, void *priv,
  84. struct v4l2_tuner *v)
  85. {
  86. if (v->index) /* Only 1 tuner */
  87. return -EINVAL;
  88. strlcpy(v->name, "FM", sizeof(v->name));
  89. v->type = V4L2_TUNER_RADIO;
  90. v->rangelow = 87*16000;
  91. v->rangehigh = 108*16000;
  92. v->signal = 0xffff;
  93. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  94. v->capability = V4L2_TUNER_CAP_LOW;
  95. v->audmode = V4L2_TUNER_MODE_MONO;
  96. return 0;
  97. }
  98. static int vidioc_s_tuner(struct file *file, void *priv,
  99. struct v4l2_tuner *v)
  100. {
  101. return v->index ? -EINVAL : 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 = f->frequency;
  120. pcm20_setfreq(dev, f->frequency);
  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. pcm20_mute(dev, ctrl->val);
  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. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
  184. goto err_hdl;
  185. v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
  186. return 0;
  187. err_hdl:
  188. v4l2_ctrl_handler_free(hdl);
  189. v4l2_device_unregister(v4l2_dev);
  190. return -EINVAL;
  191. }
  192. MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
  193. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  194. MODULE_LICENSE("GPL");
  195. static void __exit pcm20_cleanup(void)
  196. {
  197. struct pcm20 *dev = &pcm20_card;
  198. video_unregister_device(&dev->vdev);
  199. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  200. v4l2_device_unregister(&dev->v4l2_dev);
  201. }
  202. module_init(pcm20_init);
  203. module_exit(pcm20_cleanup);