]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2a1fcdf0 HV |
2 | /* |
3 | V4L2 device support. | |
4 | ||
5 | Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl> | |
6 | ||
2a1fcdf0 HV |
7 | */ |
8 | ||
9 | #include <linux/types.h> | |
10 | #include <linux/ioctl.h> | |
7a707b89 | 11 | #include <linux/module.h> |
2a1fcdf0 | 12 | #include <linux/i2c.h> |
3e0ec41c | 13 | #include <linux/slab.h> |
85e09219 DB |
14 | #if defined(CONFIG_SPI) |
15 | #include <linux/spi/spi.h> | |
16 | #endif | |
2a1fcdf0 HV |
17 | #include <linux/videodev2.h> |
18 | #include <media/v4l2-device.h> | |
11bbc1ca | 19 | #include <media/v4l2-ctrls.h> |
2a1fcdf0 HV |
20 | |
21 | int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev) | |
22 | { | |
3a63e449 | 23 | if (v4l2_dev == NULL) |
2a1fcdf0 | 24 | return -EINVAL; |
3a63e449 | 25 | |
2a1fcdf0 HV |
26 | INIT_LIST_HEAD(&v4l2_dev->subdevs); |
27 | spin_lock_init(&v4l2_dev->lock); | |
0f62fd6a | 28 | v4l2_prio_init(&v4l2_dev->prio); |
bedf8bcf | 29 | kref_init(&v4l2_dev->ref); |
236c5441 | 30 | get_device(dev); |
2a1fcdf0 | 31 | v4l2_dev->dev = dev; |
3a63e449 HV |
32 | if (dev == NULL) { |
33 | /* If dev == NULL, then name must be filled in by the caller */ | |
9e882e3b HV |
34 | if (WARN_ON(!v4l2_dev->name[0])) |
35 | return -EINVAL; | |
3a63e449 HV |
36 | return 0; |
37 | } | |
38 | ||
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", | |
c3ef01ce | 42 | dev->driver->name, dev_name(dev)); |
95db3a60 LP |
43 | if (!dev_get_drvdata(dev)) |
44 | dev_set_drvdata(dev, v4l2_dev); | |
2a1fcdf0 HV |
45 | return 0; |
46 | } | |
47 | EXPORT_SYMBOL_GPL(v4l2_device_register); | |
48 | ||
bedf8bcf HV |
49 | static void v4l2_device_release(struct kref *ref) |
50 | { | |
51 | struct v4l2_device *v4l2_dev = | |
52 | container_of(ref, struct v4l2_device, ref); | |
53 | ||
54 | if (v4l2_dev->release) | |
55 | v4l2_dev->release(v4l2_dev); | |
56 | } | |
57 | ||
58 | int v4l2_device_put(struct v4l2_device *v4l2_dev) | |
59 | { | |
60 | return kref_put(&v4l2_dev->ref, v4l2_device_release); | |
61 | } | |
62 | EXPORT_SYMBOL_GPL(v4l2_device_put); | |
63 | ||
102e7813 HV |
64 | int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename, |
65 | atomic_t *instance) | |
66 | { | |
67 | int num = atomic_inc_return(instance) - 1; | |
68 | int len = strlen(basename); | |
69 | ||
70 | if (basename[len - 1] >= '0' && basename[len - 1] <= '9') | |
71 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), | |
72 | "%s-%d", basename, num); | |
73 | else | |
74 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), | |
75 | "%s%d", basename, num); | |
76 | return num; | |
77 | } | |
78 | EXPORT_SYMBOL_GPL(v4l2_device_set_name); | |
79 | ||
ae6cfaac HV |
80 | void v4l2_device_disconnect(struct v4l2_device *v4l2_dev) |
81 | { | |
95db3a60 LP |
82 | if (v4l2_dev->dev == NULL) |
83 | return; | |
84 | ||
85 | if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev) | |
ae6cfaac | 86 | dev_set_drvdata(v4l2_dev->dev, NULL); |
236c5441 | 87 | put_device(v4l2_dev->dev); |
95db3a60 | 88 | v4l2_dev->dev = NULL; |
ae6cfaac HV |
89 | } |
90 | EXPORT_SYMBOL_GPL(v4l2_device_disconnect); | |
91 | ||
2a1fcdf0 HV |
92 | void v4l2_device_unregister(struct v4l2_device *v4l2_dev) |
93 | { | |
94 | struct v4l2_subdev *sd, *next; | |
95 | ||
9e882e3b HV |
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]) | |
2a1fcdf0 | 99 | return; |
ae6cfaac HV |
100 | v4l2_device_disconnect(v4l2_dev); |
101 | ||
3a63e449 | 102 | /* Unregister subdevs */ |
b5b2b7ed | 103 | list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) { |
2a1fcdf0 | 104 | v4l2_device_unregister_subdev(sd); |
7b34be71 | 105 | #if IS_ENABLED(CONFIG_I2C) |
b5b2b7ed HV |
106 | if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) { |
107 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
108 | ||
0d51ebd3 TF |
109 | /* |
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. | |
115 | * | |
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. | |
120 | */ | |
121 | if (client && | |
122 | !client->dev.of_node && !client->dev.fwnode) | |
b5b2b7ed | 123 | i2c_unregister_device(client); |
672dcd54 | 124 | continue; |
b5b2b7ed | 125 | } |
85e09219 DB |
126 | #endif |
127 | #if defined(CONFIG_SPI) | |
128 | if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) { | |
129 | struct spi_device *spi = v4l2_get_subdevdata(sd); | |
130 | ||
0d51ebd3 | 131 | if (spi && !spi->dev.of_node && !spi->dev.fwnode) |
85e09219 | 132 | spi_unregister_device(spi); |
672dcd54 | 133 | continue; |
85e09219 | 134 | } |
b7fd6067 | 135 | #endif |
b5b2b7ed | 136 | } |
9e882e3b HV |
137 | /* Mark as unregistered, thus preventing duplicate unregistrations */ |
138 | v4l2_dev->name[0] = '\0'; | |
2a1fcdf0 HV |
139 | } |
140 | EXPORT_SYMBOL_GPL(v4l2_device_unregister); | |
141 | ||
3a63e449 | 142 | int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev, |
61f5db54 | 143 | struct v4l2_subdev *sd) |
2a1fcdf0 | 144 | { |
61f5db54 LP |
145 | #if defined(CONFIG_MEDIA_CONTROLLER) |
146 | struct media_entity *entity = &sd->entity; | |
147 | #endif | |
11bbc1ca HV |
148 | int err; |
149 | ||
2a1fcdf0 | 150 | /* Check for valid input */ |
fc490717 | 151 | if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0]) |
2a1fcdf0 | 152 | return -EINVAL; |
2096a5dc | 153 | |
b2a06aec SA |
154 | /* |
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. | |
160 | */ | |
161 | sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver && | |
162 | sd->owner == v4l2_dev->dev->driver->owner; | |
163 | ||
164 | if (!sd->owner_v4l2_dev && !try_module_get(sd->owner)) | |
2a1fcdf0 | 165 | return -ENODEV; |
2096a5dc | 166 | |
45f6f84a | 167 | sd->v4l2_dev = v4l2_dev; |
11bbc1ca | 168 | /* This just returns 0 if either of the two args is NULL */ |
da1b1aea HV |
169 | err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, |
170 | NULL, true); | |
317efce9 | 171 | if (err) |
bdf5c198 | 172 | goto error_module; |
2096a5dc | 173 | |
61f5db54 LP |
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); | |
317efce9 | 178 | if (err < 0) |
bdf5c198 | 179 | goto error_module; |
61f5db54 LP |
180 | } |
181 | #endif | |
182 | ||
bdf5c198 SA |
183 | if (sd->internal_ops && sd->internal_ops->registered) { |
184 | err = sd->internal_ops->registered(sd); | |
185 | if (err) | |
186 | goto error_unregister; | |
187 | } | |
188 | ||
3a63e449 HV |
189 | spin_lock(&v4l2_dev->lock); |
190 | list_add_tail(&sd->list, &v4l2_dev->subdevs); | |
191 | spin_unlock(&v4l2_dev->lock); | |
2096a5dc | 192 | |
2a1fcdf0 | 193 | return 0; |
317efce9 LP |
194 | |
195 | error_unregister: | |
bdf5c198 SA |
196 | #if defined(CONFIG_MEDIA_CONTROLLER) |
197 | media_device_unregister_entity(entity); | |
198 | #endif | |
317efce9 | 199 | error_module: |
b2a06aec SA |
200 | if (!sd->owner_v4l2_dev) |
201 | module_put(sd->owner); | |
317efce9 LP |
202 | sd->v4l2_dev = NULL; |
203 | return err; | |
2a1fcdf0 HV |
204 | } |
205 | EXPORT_SYMBOL_GPL(v4l2_device_register_subdev); | |
206 | ||
0e43734d HV |
207 | static void v4l2_subdev_release(struct v4l2_subdev *sd) |
208 | { | |
209 | struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL; | |
210 | ||
211 | if (sd->internal_ops && sd->internal_ops->release) | |
212 | sd->internal_ops->release(sd); | |
213 | module_put(owner); | |
214 | } | |
215 | ||
3e0ec41c GL |
216 | static void v4l2_device_release_subdev_node(struct video_device *vdev) |
217 | { | |
0e43734d | 218 | v4l2_subdev_release(video_get_drvdata(vdev)); |
3e0ec41c GL |
219 | kfree(vdev); |
220 | } | |
221 | ||
2096a5dc LP |
222 | int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev) |
223 | { | |
224 | struct video_device *vdev; | |
225 | struct v4l2_subdev *sd; | |
226 | int err; | |
227 | ||
228 | /* Register a device node for every subdev marked with the | |
229 | * V4L2_SUBDEV_FL_HAS_DEVNODE flag. | |
230 | */ | |
231 | list_for_each_entry(sd, &v4l2_dev->subdevs, list) { | |
232 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE)) | |
233 | continue; | |
234 | ||
db0f4691 SR |
235 | if (sd->devnode) |
236 | continue; | |
237 | ||
3e0ec41c GL |
238 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); |
239 | if (!vdev) { | |
240 | err = -ENOMEM; | |
241 | goto clean_up; | |
242 | } | |
243 | ||
244 | video_set_drvdata(vdev, sd); | |
c0decac1 | 245 | strscpy(vdev->name, sd->name, sizeof(vdev->name)); |
ee494cf3 | 246 | vdev->dev_parent = sd->dev; |
2096a5dc LP |
247 | vdev->v4l2_dev = v4l2_dev; |
248 | vdev->fops = &v4l2_subdev_fops; | |
3e0ec41c | 249 | vdev->release = v4l2_device_release_subdev_node; |
18d171ba | 250 | vdev->ctrl_handler = sd->ctrl_handler; |
2096a5dc LP |
251 | err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1, |
252 | sd->owner); | |
3e0ec41c GL |
253 | if (err < 0) { |
254 | kfree(vdev); | |
255 | goto clean_up; | |
256 | } | |
909aa003 | 257 | sd->devnode = vdev; |
61f5db54 | 258 | #if defined(CONFIG_MEDIA_CONTROLLER) |
e31a0ba7 MCC |
259 | sd->entity.info.dev.major = VIDEO_MAJOR; |
260 | sd->entity.info.dev.minor = vdev->minor; | |
d9c21e3e MCC |
261 | |
262 | /* Interface is created by __video_register_device() */ | |
263 | if (vdev->v4l2_dev->mdev) { | |
264 | struct media_link *link; | |
265 | ||
266 | link = media_create_intf_link(&sd->entity, | |
267 | &vdev->intf_devnode->intf, | |
4d1e4545 HV |
268 | MEDIA_LNK_FL_ENABLED | |
269 | MEDIA_LNK_FL_IMMUTABLE); | |
1630b832 DC |
270 | if (!link) { |
271 | err = -ENOMEM; | |
d9c21e3e | 272 | goto clean_up; |
1630b832 | 273 | } |
d9c21e3e | 274 | } |
61f5db54 | 275 | #endif |
2096a5dc | 276 | } |
2096a5dc | 277 | return 0; |
3e0ec41c GL |
278 | |
279 | clean_up: | |
280 | list_for_each_entry(sd, &v4l2_dev->subdevs, list) { | |
281 | if (!sd->devnode) | |
282 | break; | |
283 | video_unregister_device(sd->devnode); | |
284 | } | |
285 | ||
286 | return err; | |
2096a5dc LP |
287 | } |
288 | EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes); | |
289 | ||
2a1fcdf0 HV |
290 | void v4l2_device_unregister_subdev(struct v4l2_subdev *sd) |
291 | { | |
61f5db54 LP |
292 | struct v4l2_device *v4l2_dev; |
293 | ||
2a1fcdf0 | 294 | /* return if it isn't registered */ |
b0167600 | 295 | if (sd == NULL || sd->v4l2_dev == NULL) |
2a1fcdf0 | 296 | return; |
2096a5dc | 297 | |
61f5db54 LP |
298 | v4l2_dev = sd->v4l2_dev; |
299 | ||
300 | spin_lock(&v4l2_dev->lock); | |
2a1fcdf0 | 301 | list_del(&sd->list); |
61f5db54 LP |
302 | spin_unlock(&v4l2_dev->lock); |
303 | ||
45f6f84a HV |
304 | if (sd->internal_ops && sd->internal_ops->unregistered) |
305 | sd->internal_ops->unregistered(sd); | |
b0167600 | 306 | sd->v4l2_dev = NULL; |
2096a5dc | 307 | |
61f5db54 | 308 | #if defined(CONFIG_MEDIA_CONTROLLER) |
c2efd3e6 | 309 | if (v4l2_dev->mdev) { |
d9c21e3e MCC |
310 | /* |
311 | * No need to explicitly remove links, as both pads and | |
312 | * links are removed by the function below, in the right order | |
313 | */ | |
61f5db54 | 314 | media_device_unregister_entity(&sd->entity); |
c2efd3e6 | 315 | } |
61f5db54 | 316 | #endif |
0e43734d HV |
317 | if (sd->devnode) |
318 | video_unregister_device(sd->devnode); | |
319 | else | |
320 | v4l2_subdev_release(sd); | |
2a1fcdf0 HV |
321 | } |
322 | EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev); |