xref: /linux/drivers/media/v4l2-core/v4l2-device.c (revision aa7b1488)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     V4L2 device support.
4 
5     Copyright (C) 2008  Hans Verkuil <hverkuil@xs4all.nl>
6 
7  */
8 
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>
16 
v4l2_device_register(struct device * dev,struct v4l2_device * v4l2_dev)17 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
18 {
19 	if (v4l2_dev == NULL)
20 		return -EINVAL;
21 
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);
26 	get_device(dev);
27 	v4l2_dev->dev = dev;
28 	if (dev == NULL) {
29 		/* If dev == NULL, then name must be filled in by the caller */
30 		if (WARN_ON(!v4l2_dev->name[0]))
31 			return -EINVAL;
32 		return 0;
33 	}
34 
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);
41 	return 0;
42 }
43 EXPORT_SYMBOL_GPL(v4l2_device_register);
44 
v4l2_device_release(struct kref * ref)45 static void v4l2_device_release(struct kref *ref)
46 {
47 	struct v4l2_device *v4l2_dev =
48 		container_of(ref, struct v4l2_device, ref);
49 
50 	if (v4l2_dev->release)
51 		v4l2_dev->release(v4l2_dev);
52 }
53 
v4l2_device_put(struct v4l2_device * v4l2_dev)54 int v4l2_device_put(struct v4l2_device *v4l2_dev)
55 {
56 	return kref_put(&v4l2_dev->ref, v4l2_device_release);
57 }
58 EXPORT_SYMBOL_GPL(v4l2_device_put);
59 
v4l2_device_set_name(struct v4l2_device * v4l2_dev,const char * basename,atomic_t * instance)60 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
61 						atomic_t *instance)
62 {
63 	int num = atomic_inc_return(instance) - 1;
64 	int len = strlen(basename);
65 
66 	if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
67 		snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
68 				"%s-%d", basename, num);
69 	else
70 		snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
71 				"%s%d", basename, num);
72 	return num;
73 }
74 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
75 
v4l2_device_disconnect(struct v4l2_device * v4l2_dev)76 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
77 {
78 	if (v4l2_dev->dev == NULL)
79 		return;
80 
81 	if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
82 		dev_set_drvdata(v4l2_dev->dev, NULL);
83 	put_device(v4l2_dev->dev);
84 	v4l2_dev->dev = NULL;
85 }
86 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
87 
v4l2_device_unregister(struct v4l2_device * v4l2_dev)88 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
89 {
90 	struct v4l2_subdev *sd, *next;
91 
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])
95 		return;
96 	v4l2_device_disconnect(v4l2_dev);
97 
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);
105 	}
106 	/* Mark as unregistered, thus preventing duplicate unregistrations */
107 	v4l2_dev->name[0] = '\0';
108 }
109 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
110 
__v4l2_device_register_subdev(struct v4l2_device * v4l2_dev,struct v4l2_subdev * sd,struct module * module)111 int __v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
112 				  struct v4l2_subdev *sd, struct module *module)
113 {
114 	int err;
115 
116 	/* Check for valid input */
117 	if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0])
118 		return -EINVAL;
119 
120 	/*
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.
126 	 */
127 	sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
128 		module == v4l2_dev->dev->driver->owner;
129 
130 	if (!sd->owner_v4l2_dev && !try_module_get(module))
131 		return -ENODEV;
132 
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,
136 				    NULL, true);
137 	if (err)
138 		goto error_module;
139 
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);
144 		if (err < 0)
145 			goto error_module;
146 	}
147 #endif
148 
149 	if (sd->internal_ops && sd->internal_ops->registered) {
150 		err = sd->internal_ops->registered(sd);
151 		if (err)
152 			goto error_unregister;
153 	}
154 
155 	sd->owner = module;
156 
157 	spin_lock(&v4l2_dev->lock);
158 	list_add_tail(&sd->list, &v4l2_dev->subdevs);
159 	spin_unlock(&v4l2_dev->lock);
160 
161 	return 0;
162 
163 error_unregister:
164 #if defined(CONFIG_MEDIA_CONTROLLER)
165 	media_device_unregister_entity(&sd->entity);
166 #endif
167 error_module:
168 	if (!sd->owner_v4l2_dev)
169 		module_put(sd->owner);
170 	sd->v4l2_dev = NULL;
171 	return err;
172 }
173 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev);
174 
v4l2_subdev_release(struct v4l2_subdev * sd)175 static void v4l2_subdev_release(struct v4l2_subdev *sd)
176 {
177 	struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL;
178 
179 	if (sd->internal_ops && sd->internal_ops->release)
180 		sd->internal_ops->release(sd);
181 	sd->devnode = NULL;
182 	module_put(owner);
183 }
184 
v4l2_device_release_subdev_node(struct video_device * vdev)185 static void v4l2_device_release_subdev_node(struct video_device *vdev)
186 {
187 	v4l2_subdev_release(video_get_drvdata(vdev));
188 	kfree(vdev);
189 }
190 
__v4l2_device_register_subdev_nodes(struct v4l2_device * v4l2_dev,bool read_only)191 int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
192 					bool read_only)
193 {
194 	struct video_device *vdev;
195 	struct v4l2_subdev *sd;
196 	int err;
197 
198 	/* Register a device node for every subdev marked with the
199 	 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
200 	 */
201 	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
202 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
203 			continue;
204 
205 		if (sd->devnode)
206 			continue;
207 
208 		vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
209 		if (!vdev) {
210 			err = -ENOMEM;
211 			goto clean_up;
212 		}
213 
214 		video_set_drvdata(vdev, sd);
215 		strscpy(vdev->name, sd->name, sizeof(vdev->name));
216 		vdev->dev_parent = sd->dev;
217 		vdev->v4l2_dev = v4l2_dev;
218 		vdev->fops = &v4l2_subdev_fops;
219 		vdev->release = v4l2_device_release_subdev_node;
220 		vdev->ctrl_handler = sd->ctrl_handler;
221 		if (read_only)
222 			set_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
223 		sd->devnode = vdev;
224 		err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
225 					      sd->owner);
226 		if (err < 0) {
227 			sd->devnode = NULL;
228 			kfree(vdev);
229 			goto clean_up;
230 		}
231 #if defined(CONFIG_MEDIA_CONTROLLER)
232 		sd->entity.info.dev.major = VIDEO_MAJOR;
233 		sd->entity.info.dev.minor = vdev->minor;
234 
235 		/* Interface is created by __video_register_device() */
236 		if (vdev->v4l2_dev->mdev) {
237 			struct media_link *link;
238 
239 			link = media_create_intf_link(&sd->entity,
240 						      &vdev->intf_devnode->intf,
241 						      MEDIA_LNK_FL_ENABLED |
242 						      MEDIA_LNK_FL_IMMUTABLE);
243 			if (!link) {
244 				err = -ENOMEM;
245 				goto clean_up;
246 			}
247 		}
248 #endif
249 	}
250 	return 0;
251 
252 clean_up:
253 	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
254 		if (!sd->devnode)
255 			break;
256 		video_unregister_device(sd->devnode);
257 	}
258 
259 	return err;
260 }
261 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
262 
v4l2_device_unregister_subdev(struct v4l2_subdev * sd)263 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
264 {
265 	struct v4l2_device *v4l2_dev;
266 
267 	/* return if it isn't registered */
268 	if (sd == NULL || sd->v4l2_dev == NULL)
269 		return;
270 
271 	v4l2_dev = sd->v4l2_dev;
272 
273 	spin_lock(&v4l2_dev->lock);
274 	list_del(&sd->list);
275 	spin_unlock(&v4l2_dev->lock);
276 
277 	if (sd->internal_ops && sd->internal_ops->unregistered)
278 		sd->internal_ops->unregistered(sd);
279 	sd->v4l2_dev = NULL;
280 
281 #if defined(CONFIG_MEDIA_CONTROLLER)
282 	if (v4l2_dev->mdev) {
283 		/*
284 		 * No need to explicitly remove links, as both pads and
285 		 * links are removed by the function below, in the right order
286 		 */
287 		media_device_unregister_entity(&sd->entity);
288 	}
289 #endif
290 	if (sd->devnode)
291 		video_unregister_device(sd->devnode);
292 	else
293 		v4l2_subdev_release(sd);
294 }
295 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);
296