1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * V4L2 controls framework Request API implementation.
8 #define pr_fmt(fmt) "v4l2-ctrls: " fmt
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <media/v4l2-ctrls.h>
13 #include <media/v4l2-dev.h>
14 #include <media/v4l2-ioctl.h>
16 #include "v4l2-ctrls-priv.h"
18 /* Initialize the request-related fields in a control handler */
19 void v4l2_ctrl_handler_init_request(struct v4l2_ctrl_handler *hdl)
21 INIT_LIST_HEAD(&hdl->requests);
22 INIT_LIST_HEAD(&hdl->requests_queued);
23 hdl->request_is_queued = false;
24 media_request_object_init(&hdl->req_obj);
27 /* Free the request-related fields in a control handler */
28 void v4l2_ctrl_handler_free_request(struct v4l2_ctrl_handler *hdl)
30 struct v4l2_ctrl_handler *req, *next_req;
33 * Do nothing if this isn't the main handler or the main
34 * handler is not used in any request.
36 * The main handler can be identified by having a NULL ops pointer in
39 if (hdl->req_obj.ops || list_empty(&hdl->requests))
43 * If the main handler is freed and it is used by handler objects in
44 * outstanding requests, then unbind and put those objects before
45 * freeing the main handler.
47 list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
48 media_request_object_unbind(&req->req_obj);
49 media_request_object_put(&req->req_obj);
53 static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
54 const struct v4l2_ctrl_handler *from)
56 struct v4l2_ctrl_ref *ref;
59 if (WARN_ON(!hdl || hdl == from))
65 WARN_ON(hdl->lock != &hdl->_lock);
67 mutex_lock(from->lock);
68 list_for_each_entry(ref, &from->ctrl_refs, node) {
69 struct v4l2_ctrl *ctrl = ref->ctrl;
70 struct v4l2_ctrl_ref *new_ref;
72 /* Skip refs inherited from other devices */
73 if (ref->from_other_dev)
75 err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
79 mutex_unlock(from->lock);
83 static void v4l2_ctrl_request_queue(struct media_request_object *obj)
85 struct v4l2_ctrl_handler *hdl =
86 container_of(obj, struct v4l2_ctrl_handler, req_obj);
87 struct v4l2_ctrl_handler *main_hdl = obj->priv;
89 mutex_lock(main_hdl->lock);
90 list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
91 hdl->request_is_queued = true;
92 mutex_unlock(main_hdl->lock);
95 static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
97 struct v4l2_ctrl_handler *hdl =
98 container_of(obj, struct v4l2_ctrl_handler, req_obj);
99 struct v4l2_ctrl_handler *main_hdl = obj->priv;
101 mutex_lock(main_hdl->lock);
102 list_del_init(&hdl->requests);
103 if (hdl->request_is_queued) {
104 list_del_init(&hdl->requests_queued);
105 hdl->request_is_queued = false;
107 mutex_unlock(main_hdl->lock);
110 static void v4l2_ctrl_request_release(struct media_request_object *obj)
112 struct v4l2_ctrl_handler *hdl =
113 container_of(obj, struct v4l2_ctrl_handler, req_obj);
115 v4l2_ctrl_handler_free(hdl);
119 static const struct media_request_object_ops req_ops = {
120 .queue = v4l2_ctrl_request_queue,
121 .unbind = v4l2_ctrl_request_unbind,
122 .release = v4l2_ctrl_request_release,
125 struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
126 struct v4l2_ctrl_handler *parent)
128 struct media_request_object *obj;
130 if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
131 req->state != MEDIA_REQUEST_STATE_QUEUED))
134 obj = media_request_object_find(req, &req_ops, parent);
136 return container_of(obj, struct v4l2_ctrl_handler, req_obj);
139 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);
142 v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
144 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
146 return (ref && ref->valid_p_req) ? ref->ctrl : NULL;
148 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);
150 static int v4l2_ctrl_request_bind(struct media_request *req,
151 struct v4l2_ctrl_handler *hdl,
152 struct v4l2_ctrl_handler *from)
156 ret = v4l2_ctrl_request_clone(hdl, from);
159 ret = media_request_object_bind(req, &req_ops,
160 from, false, &hdl->req_obj);
162 mutex_lock(from->lock);
163 list_add_tail(&hdl->requests, &from->requests);
164 mutex_unlock(from->lock);
170 static struct media_request_object *
171 v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
172 struct media_request *req, bool set)
174 struct media_request_object *obj;
175 struct v4l2_ctrl_handler *new_hdl;
179 return ERR_CAST(req);
181 if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
182 return ERR_PTR(-EBUSY);
184 obj = media_request_object_find(req, &req_ops, hdl);
188 * If there are no controls in this completed request,
189 * then that can only happen if:
191 * 1) no controls were present in the queued request, and
192 * 2) v4l2_ctrl_request_complete() could not allocate a
193 * control handler object to store the completed state in.
195 * So return ENOMEM to indicate that there was an out-of-memory
199 return ERR_PTR(-ENOMEM);
201 new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
203 return ERR_PTR(-ENOMEM);
205 obj = &new_hdl->req_obj;
206 ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
208 ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
210 v4l2_ctrl_handler_free(new_hdl);
215 media_request_object_get(obj);
219 int v4l2_g_ext_ctrls_request(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
220 struct media_device *mdev, struct v4l2_ext_controls *cs)
222 struct media_request_object *obj = NULL;
223 struct media_request *req = NULL;
226 if (!mdev || cs->request_fd < 0)
229 req = media_request_get_by_fd(mdev, cs->request_fd);
233 if (req->state != MEDIA_REQUEST_STATE_COMPLETE) {
234 media_request_put(req);
238 ret = media_request_lock_for_access(req);
240 media_request_put(req);
244 obj = v4l2_ctrls_find_req_obj(hdl, req, false);
246 media_request_unlock_for_access(req);
247 media_request_put(req);
251 hdl = container_of(obj, struct v4l2_ctrl_handler,
253 ret = v4l2_g_ext_ctrls_common(hdl, cs, vdev);
255 media_request_unlock_for_access(req);
256 media_request_object_put(obj);
257 media_request_put(req);
261 int try_set_ext_ctrls_request(struct v4l2_fh *fh,
262 struct v4l2_ctrl_handler *hdl,
263 struct video_device *vdev,
264 struct media_device *mdev,
265 struct v4l2_ext_controls *cs, bool set)
267 struct media_request_object *obj = NULL;
268 struct media_request *req = NULL;
272 dprintk(vdev, "%s: missing media device\n",
273 video_device_node_name(vdev));
277 if (cs->request_fd < 0) {
278 dprintk(vdev, "%s: invalid request fd %d\n",
279 video_device_node_name(vdev), cs->request_fd);
283 req = media_request_get_by_fd(mdev, cs->request_fd);
285 dprintk(vdev, "%s: cannot find request fd %d\n",
286 video_device_node_name(vdev), cs->request_fd);
290 ret = media_request_lock_for_update(req);
292 dprintk(vdev, "%s: cannot lock request fd %d\n",
293 video_device_node_name(vdev), cs->request_fd);
294 media_request_put(req);
298 obj = v4l2_ctrls_find_req_obj(hdl, req, set);
301 "%s: cannot find request object for request fd %d\n",
302 video_device_node_name(vdev),
304 media_request_unlock_for_update(req);
305 media_request_put(req);
309 hdl = container_of(obj, struct v4l2_ctrl_handler,
311 ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
314 "%s: try_set_ext_ctrls_common failed (%d)\n",
315 video_device_node_name(vdev), ret);
317 media_request_unlock_for_update(req);
318 media_request_object_put(obj);
319 media_request_put(req);
324 void v4l2_ctrl_request_complete(struct media_request *req,
325 struct v4l2_ctrl_handler *main_hdl)
327 struct media_request_object *obj;
328 struct v4l2_ctrl_handler *hdl;
329 struct v4l2_ctrl_ref *ref;
331 if (!req || !main_hdl)
335 * Note that it is valid if nothing was found. It means
336 * that this request doesn't have any controls and so just
337 * wants to leave the controls unchanged.
339 obj = media_request_object_find(req, &req_ops, main_hdl);
343 /* Create a new request so the driver can return controls */
344 hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
348 ret = v4l2_ctrl_handler_init(hdl, (main_hdl->nr_of_buckets - 1) * 8);
350 ret = v4l2_ctrl_request_bind(req, hdl, main_hdl);
352 v4l2_ctrl_handler_free(hdl);
356 hdl->request_is_queued = true;
357 obj = media_request_object_find(req, &req_ops, main_hdl);
359 hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
361 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
362 struct v4l2_ctrl *ctrl = ref->ctrl;
363 struct v4l2_ctrl *master = ctrl->cluster[0];
366 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
367 v4l2_ctrl_lock(master);
368 /* g_volatile_ctrl will update the current control values */
369 for (i = 0; i < master->ncontrols; i++)
370 cur_to_new(master->cluster[i]);
371 call_op(master, g_volatile_ctrl);
373 v4l2_ctrl_unlock(master);
376 if (ref->valid_p_req)
379 /* Copy the current control value into the request */
380 v4l2_ctrl_lock(ctrl);
382 v4l2_ctrl_unlock(ctrl);
385 mutex_lock(main_hdl->lock);
386 WARN_ON(!hdl->request_is_queued);
387 list_del_init(&hdl->requests_queued);
388 hdl->request_is_queued = false;
389 mutex_unlock(main_hdl->lock);
390 media_request_object_complete(obj);
391 media_request_object_put(obj);
393 EXPORT_SYMBOL(v4l2_ctrl_request_complete);
395 int v4l2_ctrl_request_setup(struct media_request *req,
396 struct v4l2_ctrl_handler *main_hdl)
398 struct media_request_object *obj;
399 struct v4l2_ctrl_handler *hdl;
400 struct v4l2_ctrl_ref *ref;
403 if (!req || !main_hdl)
406 if (WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
410 * Note that it is valid if nothing was found. It means
411 * that this request doesn't have any controls and so just
412 * wants to leave the controls unchanged.
414 obj = media_request_object_find(req, &req_ops, main_hdl);
417 if (obj->completed) {
418 media_request_object_put(obj);
421 hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
423 list_for_each_entry(ref, &hdl->ctrl_refs, node)
424 ref->req_done = false;
426 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
427 struct v4l2_ctrl *ctrl = ref->ctrl;
428 struct v4l2_ctrl *master = ctrl->cluster[0];
429 bool have_new_data = false;
433 * Skip if this control was already handled by a cluster.
434 * Skip button controls and read-only controls.
436 if (ref->req_done || (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
439 v4l2_ctrl_lock(master);
440 for (i = 0; i < master->ncontrols; i++) {
441 if (master->cluster[i]) {
442 struct v4l2_ctrl_ref *r =
443 find_ref(hdl, master->cluster[i]->id);
445 if (r->valid_p_req) {
446 have_new_data = true;
451 if (!have_new_data) {
452 v4l2_ctrl_unlock(master);
456 for (i = 0; i < master->ncontrols; i++) {
457 if (master->cluster[i]) {
458 struct v4l2_ctrl_ref *r =
459 find_ref(hdl, master->cluster[i]->id);
462 master->cluster[i]->is_new = 1;
467 * For volatile autoclusters that are currently in auto mode
468 * we need to discover if it will be set to manual mode.
469 * If so, then we have to copy the current volatile values
470 * first since those will become the new manual values (which
471 * may be overwritten by explicit new values from this set
474 if (master->is_auto && master->has_volatiles &&
475 !is_cur_manual(master)) {
476 s32 new_auto_val = *master->p_new.p_s32;
479 * If the new value == the manual value, then copy
480 * the current volatile values.
482 if (new_auto_val == master->manual_mode_value)
483 update_from_auto_cluster(master);
486 ret = try_or_set_cluster(NULL, master, true, 0);
487 v4l2_ctrl_unlock(master);
493 media_request_object_put(obj);
496 EXPORT_SYMBOL(v4l2_ctrl_request_setup);