Add a new v4l2_ctrl_request_add_handler() function that can be called in req_validate() to add a control handler request object if needed. This is needed if the driver needs to set controls in the request, Signed-off-by: Hans Verkuil <hverkuil-cisco@xxxxxxxxx> --- drivers/media/mc/mc-request.c | 3 ++- drivers/media/v4l2-core/v4l2-ctrls.c | 35 ++++++++++++++++++++++++++++ include/media/v4l2-ctrls.h | 21 +++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/drivers/media/mc/mc-request.c b/drivers/media/mc/mc-request.c index 3b013deaeb06..d66711964429 100644 --- a/drivers/media/mc/mc-request.c +++ b/drivers/media/mc/mc-request.c @@ -417,7 +417,8 @@ int media_request_object_bind(struct media_request *req, spin_lock_irqsave(&req->lock, flags); - if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING)) + if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING && + req->state != MEDIA_REQUEST_STATE_VALIDATING)) goto unlock; obj->req = req; diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c index 3f3fbcd60cc6..8a3f2b2027e4 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls.c +++ b/drivers/media/v4l2-core/v4l2-ctrls.c @@ -4345,6 +4345,41 @@ void v4l2_ctrl_request_complete(struct media_request *req, } EXPORT_SYMBOL(v4l2_ctrl_request_complete); +int v4l2_ctrl_request_add_handler(struct media_request *req, + struct v4l2_ctrl_handler *main_hdl, + bool is_ro_request) +{ + struct media_request_object *obj; + struct v4l2_ctrl_handler *new_hdl; + int ret = 0; + + if (!req || !main_hdl) + return 0; + + if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING)) + return -EBUSY; + + /* If a request object is found, then do nothing. */ + obj = media_request_object_find(req, &req_ops, main_hdl); + if (obj) { + media_request_object_put(obj); + return is_ro_request ? -EINVAL : 0; + } + + /* Create a new request so the driver can return controls */ + new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL); + if (!new_hdl) + return -ENOMEM; + + ret = v4l2_ctrl_handler_init(new_hdl, (main_hdl->nr_of_buckets - 1) * 8); + if (!ret) + ret = v4l2_ctrl_request_bind(req, new_hdl, main_hdl); + if (ret) + kfree(new_hdl); + return ret; +} +EXPORT_SYMBOL(v4l2_ctrl_request_add_handler); + int v4l2_ctrl_request_setup(struct media_request *req, struct v4l2_ctrl_handler *main_hdl) { diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h index f40e2cbb21d3..f0ee6f860798 100644 --- a/include/media/v4l2-ctrls.h +++ b/include/media/v4l2-ctrls.h @@ -1254,6 +1254,27 @@ int v4l2_ctrl_request_setup(struct media_request *req, void v4l2_ctrl_request_complete(struct media_request *req, struct v4l2_ctrl_handler *parent); +/** + * v4l2_ctrl_request_add_handler - Add a control handler request object + * + * @req: The request + * @parent: The parent control handler ('priv' in media_request_object_find()) + * @is_ro_request: this is a read-only request + * + * If the user created a request without controls, but the driver wants to + * set controls for the request, then this function can be called in the + * req_validate function. If there is no control handler object in the + * request, then this will add one. Now the driver can set controls + * and when v4l2_ctrl_request_complete() is called they will be automatically + * copied into the request. + * + * If @is_ro_request is true, then if there *is* a control handler object + * in the request, then this function will return -EINVAL. + */ +int v4l2_ctrl_request_add_handler(struct media_request *req, + struct v4l2_ctrl_handler *parent, + bool is_ro_request); + /** * v4l2_ctrl_request_hdl_find - Find the control handler in the request * -- 2.27.0