Linux Audio

Check our new training course

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