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