]>
Commit | Line | Data |
---|---|---|
137c579c HV |
1 | /* |
2 | * Framework for ISA radio drivers. | |
3 | * This takes care of all the V4L2 scaffolding, allowing the ISA drivers | |
4 | * to concentrate on the actual hardware operation. | |
5 | * | |
6 | * Copyright (C) 2012 Hans Verkuil <hans.verkuil@cisco.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
137c579c HV |
16 | */ |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/ioport.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/videodev2.h> | |
23 | #include <linux/io.h> | |
9f1dfccf | 24 | #include <linux/slab.h> |
137c579c HV |
25 | #include <media/v4l2-device.h> |
26 | #include <media/v4l2-ioctl.h> | |
27 | #include <media/v4l2-fh.h> | |
28 | #include <media/v4l2-ctrls.h> | |
29 | #include <media/v4l2-event.h> | |
30 | ||
31 | #include "radio-isa.h" | |
32 | ||
33 | MODULE_AUTHOR("Hans Verkuil"); | |
34 | MODULE_DESCRIPTION("A framework for ISA radio drivers."); | |
35 | MODULE_LICENSE("GPL"); | |
36 | ||
37 | #define FREQ_LOW (87U * 16000U) | |
38 | #define FREQ_HIGH (108U * 16000U) | |
39 | ||
40 | static int radio_isa_querycap(struct file *file, void *priv, | |
41 | struct v4l2_capability *v) | |
42 | { | |
43 | struct radio_isa_card *isa = video_drvdata(file); | |
44 | ||
45 | strlcpy(v->driver, isa->drv->driver.driver.name, sizeof(v->driver)); | |
46 | strlcpy(v->card, isa->drv->card, sizeof(v->card)); | |
47 | snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", isa->v4l2_dev.name); | |
48 | ||
8b21eb17 HV |
49 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
50 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; | |
137c579c HV |
51 | return 0; |
52 | } | |
53 | ||
54 | static int radio_isa_g_tuner(struct file *file, void *priv, | |
55 | struct v4l2_tuner *v) | |
56 | { | |
57 | struct radio_isa_card *isa = video_drvdata(file); | |
58 | const struct radio_isa_ops *ops = isa->drv->ops; | |
59 | ||
60 | if (v->index > 0) | |
61 | return -EINVAL; | |
62 | ||
63 | strlcpy(v->name, "FM", sizeof(v->name)); | |
64 | v->type = V4L2_TUNER_RADIO; | |
65 | v->rangelow = FREQ_LOW; | |
66 | v->rangehigh = FREQ_HIGH; | |
67 | v->capability = V4L2_TUNER_CAP_LOW; | |
68 | if (isa->drv->has_stereo) | |
69 | v->capability |= V4L2_TUNER_CAP_STEREO; | |
70 | ||
71 | if (ops->g_rxsubchans) | |
72 | v->rxsubchans = ops->g_rxsubchans(isa); | |
73 | else | |
74 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; | |
75 | v->audmode = isa->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO; | |
76 | if (ops->g_signal) | |
77 | v->signal = ops->g_signal(isa); | |
78 | else | |
79 | v->signal = (v->rxsubchans & V4L2_TUNER_SUB_STEREO) ? | |
80 | 0xffff : 0; | |
81 | return 0; | |
82 | } | |
83 | ||
84 | static int radio_isa_s_tuner(struct file *file, void *priv, | |
2f73c7c5 | 85 | const struct v4l2_tuner *v) |
137c579c HV |
86 | { |
87 | struct radio_isa_card *isa = video_drvdata(file); | |
88 | const struct radio_isa_ops *ops = isa->drv->ops; | |
89 | ||
90 | if (v->index) | |
91 | return -EINVAL; | |
92 | if (ops->s_stereo) { | |
93 | isa->stereo = (v->audmode == V4L2_TUNER_MODE_STEREO); | |
94 | return ops->s_stereo(isa, isa->stereo); | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
99 | static int radio_isa_s_frequency(struct file *file, void *priv, | |
b530a447 | 100 | const struct v4l2_frequency *f) |
137c579c HV |
101 | { |
102 | struct radio_isa_card *isa = video_drvdata(file); | |
b530a447 | 103 | u32 freq = f->frequency; |
137c579c HV |
104 | int res; |
105 | ||
106 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) | |
107 | return -EINVAL; | |
b530a447 HV |
108 | freq = clamp(freq, FREQ_LOW, FREQ_HIGH); |
109 | res = isa->drv->ops->s_frequency(isa, freq); | |
137c579c | 110 | if (res == 0) |
b530a447 | 111 | isa->freq = freq; |
137c579c HV |
112 | return res; |
113 | } | |
114 | ||
115 | static int radio_isa_g_frequency(struct file *file, void *priv, | |
116 | struct v4l2_frequency *f) | |
117 | { | |
118 | struct radio_isa_card *isa = video_drvdata(file); | |
119 | ||
120 | if (f->tuner != 0) | |
121 | return -EINVAL; | |
122 | f->type = V4L2_TUNER_RADIO; | |
123 | f->frequency = isa->freq; | |
124 | return 0; | |
125 | } | |
126 | ||
127 | static int radio_isa_s_ctrl(struct v4l2_ctrl *ctrl) | |
128 | { | |
129 | struct radio_isa_card *isa = | |
130 | container_of(ctrl->handler, struct radio_isa_card, hdl); | |
131 | ||
132 | switch (ctrl->id) { | |
133 | case V4L2_CID_AUDIO_MUTE: | |
134 | return isa->drv->ops->s_mute_volume(isa, ctrl->val, | |
135 | isa->volume ? isa->volume->val : 0); | |
136 | } | |
137 | return -EINVAL; | |
138 | } | |
139 | ||
140 | static int radio_isa_log_status(struct file *file, void *priv) | |
141 | { | |
142 | struct radio_isa_card *isa = video_drvdata(file); | |
143 | ||
144 | v4l2_info(&isa->v4l2_dev, "I/O Port = 0x%03x\n", isa->io); | |
145 | v4l2_ctrl_handler_log_status(&isa->hdl, isa->v4l2_dev.name); | |
146 | return 0; | |
147 | } | |
148 | ||
137c579c HV |
149 | static const struct v4l2_ctrl_ops radio_isa_ctrl_ops = { |
150 | .s_ctrl = radio_isa_s_ctrl, | |
151 | }; | |
152 | ||
153 | static const struct v4l2_file_operations radio_isa_fops = { | |
154 | .owner = THIS_MODULE, | |
155 | .open = v4l2_fh_open, | |
156 | .release = v4l2_fh_release, | |
157 | .poll = v4l2_ctrl_poll, | |
158 | .unlocked_ioctl = video_ioctl2, | |
159 | }; | |
160 | ||
161 | static const struct v4l2_ioctl_ops radio_isa_ioctl_ops = { | |
162 | .vidioc_querycap = radio_isa_querycap, | |
163 | .vidioc_g_tuner = radio_isa_g_tuner, | |
164 | .vidioc_s_tuner = radio_isa_s_tuner, | |
165 | .vidioc_g_frequency = radio_isa_g_frequency, | |
166 | .vidioc_s_frequency = radio_isa_s_frequency, | |
167 | .vidioc_log_status = radio_isa_log_status, | |
a22d85fe | 168 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
137c579c HV |
169 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
170 | }; | |
171 | ||
172 | int radio_isa_match(struct device *pdev, unsigned int dev) | |
173 | { | |
174 | struct radio_isa_driver *drv = pdev->platform_data; | |
175 | ||
176 | return drv->probe || drv->io_params[dev] >= 0; | |
177 | } | |
178 | EXPORT_SYMBOL_GPL(radio_isa_match); | |
179 | ||
180 | static bool radio_isa_valid_io(const struct radio_isa_driver *drv, int io) | |
181 | { | |
182 | int i; | |
183 | ||
184 | for (i = 0; i < drv->num_of_io_ports; i++) | |
185 | if (drv->io_ports[i] == io) | |
186 | return true; | |
187 | return false; | |
188 | } | |
189 | ||
4b644dcc | 190 | static struct radio_isa_card *radio_isa_alloc(struct radio_isa_driver *drv, |
865d7ec9 | 191 | struct device *pdev) |
137c579c | 192 | { |
137c579c | 193 | struct v4l2_device *v4l2_dev; |
865d7ec9 OZ |
194 | struct radio_isa_card *isa = drv->ops->alloc(); |
195 | if (!isa) | |
196 | return NULL; | |
137c579c | 197 | |
137c579c HV |
198 | dev_set_drvdata(pdev, isa); |
199 | isa->drv = drv; | |
137c579c HV |
200 | v4l2_dev = &isa->v4l2_dev; |
201 | strlcpy(v4l2_dev->name, dev_name(pdev), sizeof(v4l2_dev->name)); | |
202 | ||
865d7ec9 OZ |
203 | return isa; |
204 | } | |
137c579c | 205 | |
4b644dcc MCC |
206 | static int radio_isa_common_probe(struct radio_isa_card *isa, |
207 | struct device *pdev, | |
208 | int radio_nr, unsigned region_size) | |
865d7ec9 OZ |
209 | { |
210 | const struct radio_isa_driver *drv = isa->drv; | |
211 | const struct radio_isa_ops *ops = drv->ops; | |
212 | struct v4l2_device *v4l2_dev = &isa->v4l2_dev; | |
213 | int res; | |
137c579c | 214 | |
865d7ec9 | 215 | if (!request_region(isa->io, region_size, v4l2_dev->name)) { |
137c579c HV |
216 | v4l2_err(v4l2_dev, "port 0x%x already in use\n", isa->io); |
217 | kfree(isa); | |
218 | return -EBUSY; | |
219 | } | |
220 | ||
221 | res = v4l2_device_register(pdev, v4l2_dev); | |
222 | if (res < 0) { | |
223 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); | |
224 | goto err_dev_reg; | |
225 | } | |
226 | ||
227 | v4l2_ctrl_handler_init(&isa->hdl, 1); | |
228 | isa->mute = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops, | |
229 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); | |
230 | if (drv->max_volume) | |
231 | isa->volume = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops, | |
232 | V4L2_CID_AUDIO_VOLUME, 0, drv->max_volume, 1, | |
233 | drv->max_volume); | |
234 | v4l2_dev->ctrl_handler = &isa->hdl; | |
235 | if (isa->hdl.error) { | |
236 | res = isa->hdl.error; | |
237 | v4l2_err(v4l2_dev, "Could not register controls\n"); | |
238 | goto err_hdl; | |
239 | } | |
240 | if (drv->max_volume) | |
241 | v4l2_ctrl_cluster(2, &isa->mute); | |
242 | v4l2_dev->ctrl_handler = &isa->hdl; | |
243 | ||
244 | mutex_init(&isa->lock); | |
245 | isa->vdev.lock = &isa->lock; | |
246 | strlcpy(isa->vdev.name, v4l2_dev->name, sizeof(isa->vdev.name)); | |
247 | isa->vdev.v4l2_dev = v4l2_dev; | |
248 | isa->vdev.fops = &radio_isa_fops; | |
249 | isa->vdev.ioctl_ops = &radio_isa_ioctl_ops; | |
250 | isa->vdev.release = video_device_release_empty; | |
137c579c HV |
251 | video_set_drvdata(&isa->vdev, isa); |
252 | isa->freq = FREQ_LOW; | |
253 | isa->stereo = drv->has_stereo; | |
254 | ||
255 | if (ops->init) | |
256 | res = ops->init(isa); | |
257 | if (!res) | |
258 | res = v4l2_ctrl_handler_setup(&isa->hdl); | |
259 | if (!res) | |
260 | res = ops->s_frequency(isa, isa->freq); | |
261 | if (!res && ops->s_stereo) | |
262 | res = ops->s_stereo(isa, isa->stereo); | |
263 | if (res < 0) { | |
264 | v4l2_err(v4l2_dev, "Could not setup card\n"); | |
f61861fa | 265 | goto err_hdl; |
137c579c | 266 | } |
865d7ec9 OZ |
267 | res = video_register_device(&isa->vdev, VFL_TYPE_RADIO, radio_nr); |
268 | ||
137c579c HV |
269 | if (res < 0) { |
270 | v4l2_err(v4l2_dev, "Could not register device node\n"); | |
f61861fa | 271 | goto err_hdl; |
137c579c HV |
272 | } |
273 | ||
274 | v4l2_info(v4l2_dev, "Initialized radio card %s on port 0x%03x\n", | |
275 | drv->card, isa->io); | |
276 | return 0; | |
277 | ||
137c579c | 278 | err_hdl: |
f61861fa | 279 | v4l2_ctrl_handler_free(&isa->hdl); |
137c579c | 280 | err_dev_reg: |
865d7ec9 | 281 | release_region(isa->io, region_size); |
137c579c HV |
282 | kfree(isa); |
283 | return res; | |
284 | } | |
137c579c | 285 | |
4b644dcc MCC |
286 | static int radio_isa_common_remove(struct radio_isa_card *isa, |
287 | unsigned region_size) | |
137c579c | 288 | { |
137c579c HV |
289 | const struct radio_isa_ops *ops = isa->drv->ops; |
290 | ||
291 | ops->s_mute_volume(isa, true, isa->volume ? isa->volume->cur.val : 0); | |
292 | video_unregister_device(&isa->vdev); | |
293 | v4l2_ctrl_handler_free(&isa->hdl); | |
294 | v4l2_device_unregister(&isa->v4l2_dev); | |
865d7ec9 | 295 | release_region(isa->io, region_size); |
137c579c HV |
296 | v4l2_info(&isa->v4l2_dev, "Removed radio card %s\n", isa->drv->card); |
297 | kfree(isa); | |
298 | return 0; | |
299 | } | |
865d7ec9 OZ |
300 | |
301 | int radio_isa_probe(struct device *pdev, unsigned int dev) | |
302 | { | |
303 | struct radio_isa_driver *drv = pdev->platform_data; | |
304 | const struct radio_isa_ops *ops = drv->ops; | |
305 | struct v4l2_device *v4l2_dev; | |
306 | struct radio_isa_card *isa; | |
307 | ||
308 | isa = radio_isa_alloc(drv, pdev); | |
309 | if (!isa) | |
310 | return -ENOMEM; | |
311 | isa->io = drv->io_params[dev]; | |
312 | v4l2_dev = &isa->v4l2_dev; | |
313 | ||
314 | if (drv->probe && ops->probe) { | |
315 | int i; | |
316 | ||
317 | for (i = 0; i < drv->num_of_io_ports; ++i) { | |
318 | int io = drv->io_ports[i]; | |
319 | ||
320 | if (request_region(io, drv->region_size, v4l2_dev->name)) { | |
321 | bool found = ops->probe(isa, io); | |
322 | ||
323 | release_region(io, drv->region_size); | |
324 | if (found) { | |
325 | isa->io = io; | |
326 | break; | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | if (!radio_isa_valid_io(drv, isa->io)) { | |
333 | int i; | |
334 | ||
335 | if (isa->io < 0) | |
336 | return -ENODEV; | |
337 | v4l2_err(v4l2_dev, "you must set an I/O address with io=0x%03x", | |
338 | drv->io_ports[0]); | |
339 | for (i = 1; i < drv->num_of_io_ports; i++) | |
340 | printk(KERN_CONT "/0x%03x", drv->io_ports[i]); | |
341 | printk(KERN_CONT ".\n"); | |
342 | kfree(isa); | |
343 | return -EINVAL; | |
344 | } | |
345 | ||
346 | return radio_isa_common_probe(isa, pdev, drv->radio_nr_params[dev], | |
347 | drv->region_size); | |
348 | } | |
349 | EXPORT_SYMBOL_GPL(radio_isa_probe); | |
350 | ||
351 | int radio_isa_remove(struct device *pdev, unsigned int dev) | |
352 | { | |
353 | struct radio_isa_card *isa = dev_get_drvdata(pdev); | |
354 | ||
355 | return radio_isa_common_remove(isa, isa->drv->region_size); | |
356 | } | |
137c579c | 357 | EXPORT_SYMBOL_GPL(radio_isa_remove); |
865d7ec9 OZ |
358 | |
359 | #ifdef CONFIG_PNP | |
360 | int radio_isa_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id) | |
361 | { | |
362 | struct pnp_driver *pnp_drv = to_pnp_driver(dev->dev.driver); | |
363 | struct radio_isa_driver *drv = container_of(pnp_drv, | |
364 | struct radio_isa_driver, pnp_driver); | |
365 | struct radio_isa_card *isa; | |
366 | ||
367 | if (!pnp_port_valid(dev, 0)) | |
368 | return -ENODEV; | |
369 | ||
370 | isa = radio_isa_alloc(drv, &dev->dev); | |
371 | if (!isa) | |
372 | return -ENOMEM; | |
373 | ||
374 | isa->io = pnp_port_start(dev, 0); | |
375 | ||
376 | return radio_isa_common_probe(isa, &dev->dev, drv->radio_nr_params[0], | |
377 | pnp_port_len(dev, 0)); | |
378 | } | |
379 | EXPORT_SYMBOL_GPL(radio_isa_pnp_probe); | |
380 | ||
381 | void radio_isa_pnp_remove(struct pnp_dev *dev) | |
382 | { | |
383 | struct radio_isa_card *isa = dev_get_drvdata(&dev->dev); | |
384 | ||
385 | radio_isa_common_remove(isa, pnp_port_len(dev, 0)); | |
386 | } | |
387 | EXPORT_SYMBOL_GPL(radio_isa_pnp_remove); | |
388 | #endif |