1 // SPDX-License-Identifier: GPL-2.0-or-later
5 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
9 #include <linux/types.h>
10 #include <linux/ioctl.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-ctrls.h>
17 int v4l2_device_register(struct device
*dev
, struct v4l2_device
*v4l2_dev
)
22 INIT_LIST_HEAD(&v4l2_dev
->subdevs
);
23 spin_lock_init(&v4l2_dev
->lock
);
24 v4l2_prio_init(&v4l2_dev
->prio
);
25 kref_init(&v4l2_dev
->ref
);
29 /* If dev == NULL, then name must be filled in by the caller */
30 if (WARN_ON(!v4l2_dev
->name
[0]))
35 /* Set name to driver name + device name if it is empty. */
36 if (!v4l2_dev
->name
[0])
37 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
), "%s %s",
38 dev
->driver
->name
, dev_name(dev
));
39 if (!dev_get_drvdata(dev
))
40 dev_set_drvdata(dev
, v4l2_dev
);
43 EXPORT_SYMBOL_GPL(v4l2_device_register
);
45 static void v4l2_device_release(struct kref
*ref
)
47 struct v4l2_device
*v4l2_dev
=
48 container_of(ref
, struct v4l2_device
, ref
);
50 if (v4l2_dev
->release
)
51 v4l2_dev
->release(v4l2_dev
);
54 int v4l2_device_put(struct v4l2_device
*v4l2_dev
)
56 return kref_put(&v4l2_dev
->ref
, v4l2_device_release
);
58 EXPORT_SYMBOL_GPL(v4l2_device_put
);
60 int v4l2_device_set_name(struct v4l2_device
*v4l2_dev
, const char *basename
,
63 int num
= atomic_inc_return(instance
) - 1;
64 int len
= strlen(basename
);
66 if (basename
[len
- 1] >= '0' && basename
[len
- 1] <= '9')
67 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
68 "%s-%d", basename
, num
);
70 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
71 "%s%d", basename
, num
);
74 EXPORT_SYMBOL_GPL(v4l2_device_set_name
);
76 void v4l2_device_disconnect(struct v4l2_device
*v4l2_dev
)
78 if (v4l2_dev
->dev
== NULL
)
81 if (dev_get_drvdata(v4l2_dev
->dev
) == v4l2_dev
)
82 dev_set_drvdata(v4l2_dev
->dev
, NULL
);
83 put_device(v4l2_dev
->dev
);
86 EXPORT_SYMBOL_GPL(v4l2_device_disconnect
);
88 void v4l2_device_unregister(struct v4l2_device
*v4l2_dev
)
90 struct v4l2_subdev
*sd
, *next
;
92 /* Just return if v4l2_dev is NULL or if it was already
93 * unregistered before. */
94 if (v4l2_dev
== NULL
|| !v4l2_dev
->name
[0])
96 v4l2_device_disconnect(v4l2_dev
);
98 /* Unregister subdevs */
99 list_for_each_entry_safe(sd
, next
, &v4l2_dev
->subdevs
, list
) {
100 v4l2_device_unregister_subdev(sd
);
101 if (sd
->flags
& V4L2_SUBDEV_FL_IS_I2C
)
102 v4l2_i2c_subdev_unregister(sd
);
103 else if (sd
->flags
& V4L2_SUBDEV_FL_IS_SPI
)
104 v4l2_spi_subdev_unregister(sd
);
106 /* Mark as unregistered, thus preventing duplicate unregistrations */
107 v4l2_dev
->name
[0] = '\0';
109 EXPORT_SYMBOL_GPL(v4l2_device_unregister
);
111 int v4l2_device_register_subdev(struct v4l2_device
*v4l2_dev
,
112 struct v4l2_subdev
*sd
)
116 /* Check for valid input */
117 if (!v4l2_dev
|| !sd
|| sd
->v4l2_dev
|| !sd
->name
[0])
121 * The reason to acquire the module here is to avoid unloading
122 * a module of sub-device which is registered to a media
123 * device. To make it possible to unload modules for media
124 * devices that also register sub-devices, do not
125 * try_module_get() such sub-device owners.
127 sd
->owner_v4l2_dev
= v4l2_dev
->dev
&& v4l2_dev
->dev
->driver
&&
128 sd
->owner
== v4l2_dev
->dev
->driver
->owner
;
130 if (!sd
->owner_v4l2_dev
&& !try_module_get(sd
->owner
))
133 sd
->v4l2_dev
= v4l2_dev
;
134 /* This just returns 0 if either of the two args is NULL */
135 err
= v4l2_ctrl_add_handler(v4l2_dev
->ctrl_handler
, sd
->ctrl_handler
,
140 #if defined(CONFIG_MEDIA_CONTROLLER)
141 /* Register the entity. */
142 if (v4l2_dev
->mdev
) {
143 err
= media_device_register_entity(v4l2_dev
->mdev
, &sd
->entity
);
149 if (sd
->internal_ops
&& sd
->internal_ops
->registered
) {
150 err
= sd
->internal_ops
->registered(sd
);
152 goto error_unregister
;
155 spin_lock(&v4l2_dev
->lock
);
156 list_add_tail(&sd
->list
, &v4l2_dev
->subdevs
);
157 spin_unlock(&v4l2_dev
->lock
);
162 #if defined(CONFIG_MEDIA_CONTROLLER)
163 media_device_unregister_entity(&sd
->entity
);
166 if (!sd
->owner_v4l2_dev
)
167 module_put(sd
->owner
);
171 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev
);
173 static void v4l2_subdev_release(struct v4l2_subdev
*sd
)
175 struct module
*owner
= !sd
->owner_v4l2_dev
? sd
->owner
: NULL
;
177 if (sd
->internal_ops
&& sd
->internal_ops
->release
)
178 sd
->internal_ops
->release(sd
);
183 static void v4l2_device_release_subdev_node(struct video_device
*vdev
)
185 v4l2_subdev_release(video_get_drvdata(vdev
));
189 int __v4l2_device_register_subdev_nodes(struct v4l2_device
*v4l2_dev
,
192 struct video_device
*vdev
;
193 struct v4l2_subdev
*sd
;
196 /* Register a device node for every subdev marked with the
197 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
199 list_for_each_entry(sd
, &v4l2_dev
->subdevs
, list
) {
200 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_DEVNODE
))
206 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
212 video_set_drvdata(vdev
, sd
);
213 strscpy(vdev
->name
, sd
->name
, sizeof(vdev
->name
));
214 vdev
->dev_parent
= sd
->dev
;
215 vdev
->v4l2_dev
= v4l2_dev
;
216 vdev
->fops
= &v4l2_subdev_fops
;
217 vdev
->release
= v4l2_device_release_subdev_node
;
218 vdev
->ctrl_handler
= sd
->ctrl_handler
;
220 set_bit(V4L2_FL_SUBDEV_RO_DEVNODE
, &vdev
->flags
);
222 err
= __video_register_device(vdev
, VFL_TYPE_SUBDEV
, -1, 1,
229 #if defined(CONFIG_MEDIA_CONTROLLER)
230 sd
->entity
.info
.dev
.major
= VIDEO_MAJOR
;
231 sd
->entity
.info
.dev
.minor
= vdev
->minor
;
233 /* Interface is created by __video_register_device() */
234 if (vdev
->v4l2_dev
->mdev
) {
235 struct media_link
*link
;
237 link
= media_create_intf_link(&sd
->entity
,
238 &vdev
->intf_devnode
->intf
,
239 MEDIA_LNK_FL_ENABLED
|
240 MEDIA_LNK_FL_IMMUTABLE
);
251 list_for_each_entry(sd
, &v4l2_dev
->subdevs
, list
) {
254 video_unregister_device(sd
->devnode
);
259 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes
);
261 void v4l2_device_unregister_subdev(struct v4l2_subdev
*sd
)
263 struct v4l2_device
*v4l2_dev
;
265 /* return if it isn't registered */
266 if (sd
== NULL
|| sd
->v4l2_dev
== NULL
)
269 v4l2_dev
= sd
->v4l2_dev
;
271 spin_lock(&v4l2_dev
->lock
);
273 spin_unlock(&v4l2_dev
->lock
);
275 if (sd
->internal_ops
&& sd
->internal_ops
->unregistered
)
276 sd
->internal_ops
->unregistered(sd
);
279 #if defined(CONFIG_MEDIA_CONTROLLER)
280 if (v4l2_dev
->mdev
) {
282 * No need to explicitly remove links, as both pads and
283 * links are removed by the function below, in the right order
285 media_device_unregister_entity(&sd
->entity
);
289 video_unregister_device(sd
->devnode
);
291 v4l2_subdev_release(sd
);
293 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev
);