1/*
2    V4L2 device support.
3
4    Copyright (C) 2008  Hans Verkuil <hverkuil@xs4all.nl>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#include <linux/types.h>
22#include <linux/ioctl.h>
23#include <linux/module.h>
24#include <linux/i2c.h>
25#include <linux/slab.h>
26#if defined(CONFIG_SPI)
27#include <linux/spi/spi.h>
28#endif
29#include <linux/videodev2.h>
30#include <media/v4l2-device.h>
31#include <media/v4l2-ctrls.h>
32
33int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
34{
35	if (v4l2_dev == NULL)
36		return -EINVAL;
37
38	INIT_LIST_HEAD(&v4l2_dev->subdevs);
39	spin_lock_init(&v4l2_dev->lock);
40	v4l2_prio_init(&v4l2_dev->prio);
41	kref_init(&v4l2_dev->ref);
42	get_device(dev);
43	v4l2_dev->dev = dev;
44	if (dev == NULL) {
45		/* If dev == NULL, then name must be filled in by the caller */
46		if (WARN_ON(!v4l2_dev->name[0]))
47			return -EINVAL;
48		return 0;
49	}
50
51	/* Set name to driver name + device name if it is empty. */
52	if (!v4l2_dev->name[0])
53		snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
54			dev->driver->name, dev_name(dev));
55	if (!dev_get_drvdata(dev))
56		dev_set_drvdata(dev, v4l2_dev);
57	return 0;
58}
59EXPORT_SYMBOL_GPL(v4l2_device_register);
60
61static void v4l2_device_release(struct kref *ref)
62{
63	struct v4l2_device *v4l2_dev =
64		container_of(ref, struct v4l2_device, ref);
65
66	if (v4l2_dev->release)
67		v4l2_dev->release(v4l2_dev);
68}
69
70int v4l2_device_put(struct v4l2_device *v4l2_dev)
71{
72	return kref_put(&v4l2_dev->ref, v4l2_device_release);
73}
74EXPORT_SYMBOL_GPL(v4l2_device_put);
75
76int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
77						atomic_t *instance)
78{
79	int num = atomic_inc_return(instance) - 1;
80	int len = strlen(basename);
81
82	if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
83		snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
84				"%s-%d", basename, num);
85	else
86		snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
87				"%s%d", basename, num);
88	return num;
89}
90EXPORT_SYMBOL_GPL(v4l2_device_set_name);
91
92void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
93{
94	if (v4l2_dev->dev == NULL)
95		return;
96
97	if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
98		dev_set_drvdata(v4l2_dev->dev, NULL);
99	put_device(v4l2_dev->dev);
100	v4l2_dev->dev = NULL;
101}
102EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
103
104void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
105{
106	struct v4l2_subdev *sd, *next;
107
108	/* Just return if v4l2_dev is NULL or if it was already
109	 * unregistered before. */
110	if (v4l2_dev == NULL || !v4l2_dev->name[0])
111		return;
112	v4l2_device_disconnect(v4l2_dev);
113
114	/* Unregister subdevs */
115	list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
116		v4l2_device_unregister_subdev(sd);
117#if IS_ENABLED(CONFIG_I2C)
118		if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
119			struct i2c_client *client = v4l2_get_subdevdata(sd);
120
121			/* We need to unregister the i2c client explicitly.
122			   We cannot rely on i2c_del_adapter to always
123			   unregister clients for us, since if the i2c bus
124			   is a platform bus, then it is never deleted. */
125			if (client)
126				i2c_unregister_device(client);
127			continue;
128		}
129#endif
130#if defined(CONFIG_SPI)
131		if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
132			struct spi_device *spi = v4l2_get_subdevdata(sd);
133
134			if (spi)
135				spi_unregister_device(spi);
136			continue;
137		}
138#endif
139	}
140	/* Mark as unregistered, thus preventing duplicate unregistrations */
141	v4l2_dev->name[0] = '\0';
142}
143EXPORT_SYMBOL_GPL(v4l2_device_unregister);
144
145int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
146				struct v4l2_subdev *sd)
147{
148#if defined(CONFIG_MEDIA_CONTROLLER)
149	struct media_entity *entity = &sd->entity;
150#endif
151	int err;
152
153	/* Check for valid input */
154	if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
155		return -EINVAL;
156
157	/* Warn if we apparently re-register a subdev */
158	WARN_ON(sd->v4l2_dev != NULL);
159
160	/*
161	 * The reason to acquire the module here is to avoid unloading
162	 * a module of sub-device which is registered to a media
163	 * device. To make it possible to unload modules for media
164	 * devices that also register sub-devices, do not
165	 * try_module_get() such sub-device owners.
166	 */
167	sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
168		sd->owner == v4l2_dev->dev->driver->owner;
169
170	if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
171		return -ENODEV;
172
173	sd->v4l2_dev = v4l2_dev;
174	if (sd->internal_ops && sd->internal_ops->registered) {
175		err = sd->internal_ops->registered(sd);
176		if (err)
177			goto error_module;
178	}
179
180	/* This just returns 0 if either of the two args is NULL */
181	err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
182	if (err)
183		goto error_unregister;
184
185#if defined(CONFIG_MEDIA_CONTROLLER)
186	/* Register the entity. */
187	if (v4l2_dev->mdev) {
188		err = media_device_register_entity(v4l2_dev->mdev, entity);
189		if (err < 0)
190			goto error_unregister;
191	}
192#endif
193
194	spin_lock(&v4l2_dev->lock);
195	list_add_tail(&sd->list, &v4l2_dev->subdevs);
196	spin_unlock(&v4l2_dev->lock);
197
198	return 0;
199
200error_unregister:
201	if (sd->internal_ops && sd->internal_ops->unregistered)
202		sd->internal_ops->unregistered(sd);
203error_module:
204	if (!sd->owner_v4l2_dev)
205		module_put(sd->owner);
206	sd->v4l2_dev = NULL;
207	return err;
208}
209EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
210
211static void v4l2_device_release_subdev_node(struct video_device *vdev)
212{
213	struct v4l2_subdev *sd = video_get_drvdata(vdev);
214	sd->devnode = NULL;
215	kfree(vdev);
216}
217
218int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
219{
220	struct video_device *vdev;
221	struct v4l2_subdev *sd;
222	int err;
223
224	/* Register a device node for every subdev marked with the
225	 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
226	 */
227	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
228		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
229			continue;
230
231		vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
232		if (!vdev) {
233			err = -ENOMEM;
234			goto clean_up;
235		}
236
237		video_set_drvdata(vdev, sd);
238		strlcpy(vdev->name, sd->name, sizeof(vdev->name));
239		vdev->v4l2_dev = v4l2_dev;
240		vdev->fops = &v4l2_subdev_fops;
241		vdev->release = v4l2_device_release_subdev_node;
242		vdev->ctrl_handler = sd->ctrl_handler;
243		err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
244					      sd->owner);
245		if (err < 0) {
246			kfree(vdev);
247			goto clean_up;
248		}
249#if defined(CONFIG_MEDIA_CONTROLLER)
250		sd->entity.info.dev.major = VIDEO_MAJOR;
251		sd->entity.info.dev.minor = vdev->minor;
252#endif
253		sd->devnode = vdev;
254	}
255	return 0;
256
257clean_up:
258	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
259		if (!sd->devnode)
260			break;
261		video_unregister_device(sd->devnode);
262	}
263
264	return err;
265}
266EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
267
268void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
269{
270	struct v4l2_device *v4l2_dev;
271
272	/* return if it isn't registered */
273	if (sd == NULL || sd->v4l2_dev == NULL)
274		return;
275
276	v4l2_dev = sd->v4l2_dev;
277
278	spin_lock(&v4l2_dev->lock);
279	list_del(&sd->list);
280	spin_unlock(&v4l2_dev->lock);
281
282	if (sd->internal_ops && sd->internal_ops->unregistered)
283		sd->internal_ops->unregistered(sd);
284	sd->v4l2_dev = NULL;
285
286#if defined(CONFIG_MEDIA_CONTROLLER)
287	if (v4l2_dev->mdev) {
288		media_entity_remove_links(&sd->entity);
289		media_device_unregister_entity(&sd->entity);
290	}
291#endif
292	video_unregister_device(sd->devnode);
293	if (!sd->owner_v4l2_dev)
294		module_put(sd->owner);
295}
296EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);
297