1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include <linux/types.h>
10 #include <linux/ioctl.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #if defined(CONFIG_SPI)
15 #include <linux/spi/spi.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ctrls.h>
21 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
26 INIT_LIST_HEAD(&v4l2_dev->subdevs);
27 spin_lock_init(&v4l2_dev->lock);
28 v4l2_prio_init(&v4l2_dev->prio);
29 kref_init(&v4l2_dev->ref);
33 /* If dev == NULL, then name must be filled in by the caller */
34 if (WARN_ON(!v4l2_dev->name[0]))
39 /* Set name to driver name + device name if it is empty. */
40 if (!v4l2_dev->name[0])
41 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
42 dev->driver->name, dev_name(dev));
43 if (!dev_get_drvdata(dev))
44 dev_set_drvdata(dev, v4l2_dev);
47 EXPORT_SYMBOL_GPL(v4l2_device_register);
49 static void v4l2_device_release(struct kref *ref)
51 struct v4l2_device *v4l2_dev =
52 container_of(ref, struct v4l2_device, ref);
54 if (v4l2_dev->release)
55 v4l2_dev->release(v4l2_dev);
58 int v4l2_device_put(struct v4l2_device *v4l2_dev)
60 return kref_put(&v4l2_dev->ref, v4l2_device_release);
62 EXPORT_SYMBOL_GPL(v4l2_device_put);
64 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
67 int num = atomic_inc_return(instance) - 1;
68 int len = strlen(basename);
70 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
71 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
72 "%s-%d", basename, num);
74 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
75 "%s%d", basename, num);
78 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
80 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
82 if (v4l2_dev->dev == NULL)
85 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
86 dev_set_drvdata(v4l2_dev->dev, NULL);
87 put_device(v4l2_dev->dev);
90 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
92 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
94 struct v4l2_subdev *sd, *next;
96 /* Just return if v4l2_dev is NULL or if it was already
97 * unregistered before. */
98 if (v4l2_dev == NULL || !v4l2_dev->name[0])
100 v4l2_device_disconnect(v4l2_dev);
102 /* Unregister subdevs */
103 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
104 v4l2_device_unregister_subdev(sd);
105 #if IS_ENABLED(CONFIG_I2C)
106 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
107 struct i2c_client *client = v4l2_get_subdevdata(sd);
110 * We need to unregister the i2c client
111 * explicitly. We cannot rely on
112 * i2c_del_adapter to always unregister
113 * clients for us, since if the i2c bus is a
114 * platform bus, then it is never deleted.
116 * Device tree or ACPI based devices must not
117 * be unregistered as they have not been
118 * registered by us, and would not be
119 * re-created by just probing the V4L2 driver.
122 !client->dev.of_node && !client->dev.fwnode)
123 i2c_unregister_device(client);
127 #if defined(CONFIG_SPI)
128 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
129 struct spi_device *spi = v4l2_get_subdevdata(sd);
131 if (spi && !spi->dev.of_node && !spi->dev.fwnode)
132 spi_unregister_device(spi);
137 /* Mark as unregistered, thus preventing duplicate unregistrations */
138 v4l2_dev->name[0] = '\0';
140 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
142 int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
143 struct v4l2_subdev *sd)
145 #if defined(CONFIG_MEDIA_CONTROLLER)
146 struct media_entity *entity = &sd->entity;
150 /* Check for valid input */
151 if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0])
155 * The reason to acquire the module here is to avoid unloading
156 * a module of sub-device which is registered to a media
157 * device. To make it possible to unload modules for media
158 * devices that also register sub-devices, do not
159 * try_module_get() such sub-device owners.
161 sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
162 sd->owner == v4l2_dev->dev->driver->owner;
164 if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
167 sd->v4l2_dev = v4l2_dev;
168 /* This just returns 0 if either of the two args is NULL */
169 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler,
174 #if defined(CONFIG_MEDIA_CONTROLLER)
175 /* Register the entity. */
176 if (v4l2_dev->mdev) {
177 err = media_device_register_entity(v4l2_dev->mdev, entity);
183 if (sd->internal_ops && sd->internal_ops->registered) {
184 err = sd->internal_ops->registered(sd);
186 goto error_unregister;
189 spin_lock(&v4l2_dev->lock);
190 list_add_tail(&sd->list, &v4l2_dev->subdevs);
191 spin_unlock(&v4l2_dev->lock);
196 #if defined(CONFIG_MEDIA_CONTROLLER)
197 media_device_unregister_entity(entity);
200 if (!sd->owner_v4l2_dev)
201 module_put(sd->owner);
205 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
207 static void v4l2_subdev_release(struct v4l2_subdev *sd)
209 struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL;
211 if (sd->internal_ops && sd->internal_ops->release)
212 sd->internal_ops->release(sd);
216 static void v4l2_device_release_subdev_node(struct video_device *vdev)
218 v4l2_subdev_release(video_get_drvdata(vdev));
222 int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
224 struct video_device *vdev;
225 struct v4l2_subdev *sd;
228 /* Register a device node for every subdev marked with the
229 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
231 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
232 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
238 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
244 video_set_drvdata(vdev, sd);
245 strscpy(vdev->name, sd->name, sizeof(vdev->name));
246 vdev->dev_parent = sd->dev;
247 vdev->v4l2_dev = v4l2_dev;
248 vdev->fops = &v4l2_subdev_fops;
249 vdev->release = v4l2_device_release_subdev_node;
250 vdev->ctrl_handler = sd->ctrl_handler;
251 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
258 #if defined(CONFIG_MEDIA_CONTROLLER)
259 sd->entity.info.dev.major = VIDEO_MAJOR;
260 sd->entity.info.dev.minor = vdev->minor;
262 /* Interface is created by __video_register_device() */
263 if (vdev->v4l2_dev->mdev) {
264 struct media_link *link;
266 link = media_create_intf_link(&sd->entity,
267 &vdev->intf_devnode->intf,
268 MEDIA_LNK_FL_ENABLED |
269 MEDIA_LNK_FL_IMMUTABLE);
280 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
283 video_unregister_device(sd->devnode);
288 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
290 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
292 struct v4l2_device *v4l2_dev;
294 /* return if it isn't registered */
295 if (sd == NULL || sd->v4l2_dev == NULL)
298 v4l2_dev = sd->v4l2_dev;
300 spin_lock(&v4l2_dev->lock);
302 spin_unlock(&v4l2_dev->lock);
304 if (sd->internal_ops && sd->internal_ops->unregistered)
305 sd->internal_ops->unregistered(sd);
308 #if defined(CONFIG_MEDIA_CONTROLLER)
309 if (v4l2_dev->mdev) {
311 * No need to explicitly remove links, as both pads and
312 * links are removed by the function below, in the right order
314 media_device_unregister_entity(&sd->entity);
318 video_unregister_device(sd->devnode);
320 v4l2_subdev_release(sd);
322 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);