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