1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * V4L2 controls framework uAPI implementation:
8 #define pr_fmt(fmt) "v4l2-ctrls: " fmt
10 #include <linux/export.h>
12 #include <linux/slab.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-dev.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-event.h>
17 #include <media/v4l2-ioctl.h>
19 #include "v4l2-ctrls-priv.h"
21 /* Internal temporary helper struct, one for each v4l2_ext_control */
22 struct v4l2_ctrl_helper {
23 /* Pointer to the control reference of the master control */
24 struct v4l2_ctrl_ref *mref;
25 /* The control ref corresponding to the v4l2_ext_control ID field. */
26 struct v4l2_ctrl_ref *ref;
28 * v4l2_ext_control index of the next control belonging to the
29 * same cluster, or 0 if there isn't any.
35 * Helper functions to copy control payload data from kernel space to
36 * user space and vice versa.
39 /* Helper function: copy the given control value back to the caller */
40 static int ptr_to_user(struct v4l2_ext_control *c,
41 struct v4l2_ctrl *ctrl,
42 union v4l2_ctrl_ptr ptr)
46 if (ctrl->is_ptr && !ctrl->is_string)
47 return copy_to_user(c->ptr, ptr.p_const, c->size) ?
51 case V4L2_CTRL_TYPE_STRING:
52 len = strlen(ptr.p_char);
53 if (c->size < len + 1) {
54 c->size = ctrl->elem_size;
57 return copy_to_user(c->string, ptr.p_char, len + 1) ?
59 case V4L2_CTRL_TYPE_INTEGER64:
60 c->value64 = *ptr.p_s64;
63 c->value = *ptr.p_s32;
69 /* Helper function: copy the current control value back to the caller */
70 static int cur_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
72 return ptr_to_user(c, ctrl, ctrl->p_cur);
75 /* Helper function: copy the new control value back to the caller */
76 static int new_to_user(struct v4l2_ext_control *c,
77 struct v4l2_ctrl *ctrl)
79 return ptr_to_user(c, ctrl, ctrl->p_new);
82 /* Helper function: copy the request value back to the caller */
83 static int req_to_user(struct v4l2_ext_control *c,
84 struct v4l2_ctrl_ref *ref)
86 return ptr_to_user(c, ref->ctrl, ref->p_req);
89 /* Helper function: copy the initial control value back to the caller */
90 static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
94 for (idx = 0; idx < ctrl->elems; idx++)
95 ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
97 return ptr_to_user(c, ctrl, ctrl->p_new);
100 /* Helper function: copy the caller-provider value as the new control value */
101 static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
107 if (ctrl->is_dyn_array &&
108 c->size > ctrl->p_dyn_alloc_elems * ctrl->elem_size) {
109 void *old = ctrl->p_dyn;
110 void *tmp = kvzalloc(2 * c->size, GFP_KERNEL);
114 memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
115 memcpy(tmp + c->size, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
117 ctrl->p_cur.p = tmp + c->size;
119 ctrl->p_dyn_alloc_elems = c->size / ctrl->elem_size;
123 if (ctrl->is_ptr && !ctrl->is_string) {
124 unsigned int elems = c->size / ctrl->elem_size;
127 if (copy_from_user(ctrl->p_new.p, c->ptr, c->size))
130 if (ctrl->is_dyn_array)
131 ctrl->new_elems = elems;
132 else if (ctrl->is_array)
133 for (idx = elems; idx < ctrl->elems; idx++)
134 ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
138 switch (ctrl->type) {
139 case V4L2_CTRL_TYPE_INTEGER64:
140 *ctrl->p_new.p_s64 = c->value64;
142 case V4L2_CTRL_TYPE_STRING:
146 if (size > ctrl->maximum + 1)
147 size = ctrl->maximum + 1;
148 ret = copy_from_user(ctrl->p_new.p_char, c->string, size) ? -EFAULT : 0;
150 char last = ctrl->p_new.p_char[size - 1];
152 ctrl->p_new.p_char[size - 1] = 0;
154 * If the string was longer than ctrl->maximum,
155 * then return an error.
157 if (strlen(ctrl->p_new.p_char) == ctrl->maximum && last)
162 *ctrl->p_new.p_s32 = c->value;
170 * VIDIOC_G/TRY/S_EXT_CTRLS implementation
174 * Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
176 * It is not a fully atomic operation, just best-effort only. After all, if
177 * multiple controls have to be set through multiple i2c writes (for example)
178 * then some initial writes may succeed while others fail. Thus leaving the
179 * system in an inconsistent state. The question is how much effort you are
180 * willing to spend on trying to make something atomic that really isn't.
182 * From the point of view of an application the main requirement is that
183 * when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
184 * error should be returned without actually affecting any controls.
186 * If all the values are correct, then it is acceptable to just give up
187 * in case of low-level errors.
189 * It is important though that the application can tell when only a partial
190 * configuration was done. The way we do that is through the error_idx field
191 * of struct v4l2_ext_controls: if that is equal to the count field then no
192 * controls were affected. Otherwise all controls before that index were
193 * successful in performing their 'get' or 'set' operation, the control at
194 * the given index failed, and you don't know what happened with the controls
195 * after the failed one. Since if they were part of a control cluster they
196 * could have been successfully processed (if a cluster member was encountered
197 * at index < error_idx), they could have failed (if a cluster member was at
198 * error_idx), or they may not have been processed yet (if the first cluster
199 * member appeared after error_idx).
201 * It is all fairly theoretical, though. In practice all you can do is to
202 * bail out. If error_idx == count, then it is an application bug. If
203 * error_idx < count then it is only an application bug if the error code was
204 * EBUSY. That usually means that something started streaming just when you
205 * tried to set the controls. In all other cases it is a driver/hardware
206 * problem and all you can do is to retry or bail out.
208 * Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
209 * never modifies controls the error_idx is just set to whatever control
210 * has an invalid value.
214 * Prepare for the extended g/s/try functions.
215 * Find the controls in the control array and do some basic checks.
217 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
218 struct v4l2_ext_controls *cs,
219 struct v4l2_ctrl_helper *helpers,
220 struct video_device *vdev,
223 struct v4l2_ctrl_helper *h;
224 bool have_clusters = false;
227 for (i = 0, h = helpers; i < cs->count; i++, h++) {
228 struct v4l2_ext_control *c = &cs->controls[i];
229 struct v4l2_ctrl_ref *ref;
230 struct v4l2_ctrl *ctrl;
231 u32 id = c->id & V4L2_CTRL_ID_MASK;
236 cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
237 cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
238 V4L2_CTRL_ID2WHICH(id) != cs->which) {
240 "invalid which 0x%x or control id 0x%x\n",
246 * Old-style private controls are not allowed for
249 if (id >= V4L2_CID_PRIVATE_BASE) {
251 "old-style private controls not allowed\n");
254 ref = find_ref_lock(hdl, id);
256 dprintk(vdev, "cannot find control id 0x%x\n", id);
261 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
262 dprintk(vdev, "control id 0x%x is disabled\n", id);
266 if (ctrl->cluster[0]->ncontrols > 1)
267 have_clusters = true;
268 if (ctrl->cluster[0] != ctrl)
269 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
270 if (ctrl->is_dyn_array) {
271 unsigned int max_size = ctrl->dims[0] * ctrl->elem_size;
272 unsigned int tot_size = ctrl->elem_size;
274 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
275 tot_size *= ref->p_req_elems;
277 tot_size *= ctrl->elems;
279 c->size = ctrl->elem_size * (c->size / ctrl->elem_size);
281 if (c->size < tot_size) {
287 if (c->size > max_size) {
294 } else if (ctrl->is_ptr && !ctrl->is_string) {
295 unsigned int tot_size = ctrl->elems * ctrl->elem_size;
297 if (c->size < tot_size) {
299 * In the get case the application first
300 * queries to obtain the size of the control.
307 "pointer control id 0x%x size too small, %d bytes but %d bytes needed\n",
308 id, c->size, tot_size);
313 /* Store the ref to the master control of the cluster */
316 * Initially set next to 0, meaning that there is no other
317 * control in this helper array belonging to the same
324 * We are done if there were no controls that belong to a multi-
331 * The code below figures out in O(n) time which controls in the list
332 * belong to the same cluster.
335 /* This has to be done with the handler lock taken. */
336 mutex_lock(hdl->lock);
338 /* First zero the helper field in the master control references */
339 for (i = 0; i < cs->count; i++)
340 helpers[i].mref->helper = NULL;
341 for (i = 0, h = helpers; i < cs->count; i++, h++) {
342 struct v4l2_ctrl_ref *mref = h->mref;
345 * If the mref->helper is set, then it points to an earlier
346 * helper that belongs to the same cluster.
350 * Set the next field of mref->helper to the current
351 * index: this means that the earlier helper now
352 * points to the next helper in the same cluster.
354 mref->helper->next = i;
356 * mref should be set only for the first helper in the
357 * cluster, clear the others.
361 /* Point the mref helper to the current helper struct. */
364 mutex_unlock(hdl->lock);
369 * Handles the corner case where cs->count == 0. It checks whether the
370 * specified control class exists. If that class ID is 0, then it checks
371 * whether there are any controls at all.
373 static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
375 if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL ||
376 which == V4L2_CTRL_WHICH_REQUEST_VAL)
378 return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
382 * Get extended controls. Allocates the helpers array if needed.
384 * Note that v4l2_g_ext_ctrls_common() with 'which' set to
385 * V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was
386 * completed, and in that case p_req_valid is true for all controls.
388 int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
389 struct v4l2_ext_controls *cs,
390 struct video_device *vdev)
392 struct v4l2_ctrl_helper helper[4];
393 struct v4l2_ctrl_helper *helpers = helper;
396 bool is_default, is_request;
398 is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
399 is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL);
401 cs->error_idx = cs->count;
402 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
408 return class_check(hdl, cs->which);
410 if (cs->count > ARRAY_SIZE(helper)) {
411 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
417 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, true);
418 cs->error_idx = cs->count;
420 for (i = 0; !ret && i < cs->count; i++)
421 if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
424 for (i = 0; !ret && i < cs->count; i++) {
425 struct v4l2_ctrl *master;
426 bool is_volatile = false;
429 if (!helpers[i].mref)
432 master = helpers[i].mref->ctrl;
435 v4l2_ctrl_lock(master);
438 * g_volatile_ctrl will update the new control values.
439 * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL and
440 * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests
441 * it is v4l2_ctrl_request_complete() that copies the
442 * volatile controls at the time of request completion
443 * to the request, so you don't want to do that again.
445 if (!is_default && !is_request &&
446 ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
447 (master->has_volatiles && !is_cur_manual(master)))) {
448 for (j = 0; j < master->ncontrols; j++)
449 cur_to_new(master->cluster[j]);
450 ret = call_op(master, g_volatile_ctrl);
455 v4l2_ctrl_unlock(master);
460 * Copy the default value (if is_default is true), the
461 * request value (if is_request is true and p_req is valid),
462 * the new volatile value (if is_volatile is true) or the
466 struct v4l2_ctrl_ref *ref = helpers[idx].ref;
469 ret = def_to_user(cs->controls + idx, ref->ctrl);
470 else if (is_request && ref->p_req_dyn_enomem)
472 else if (is_request && ref->p_req_valid)
473 ret = req_to_user(cs->controls + idx, ref);
474 else if (is_volatile)
475 ret = new_to_user(cs->controls + idx, ref->ctrl);
477 ret = cur_to_user(cs->controls + idx, ref->ctrl);
478 idx = helpers[idx].next;
479 } while (!ret && idx);
481 v4l2_ctrl_unlock(master);
484 if (cs->count > ARRAY_SIZE(helper))
489 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
490 struct media_device *mdev, struct v4l2_ext_controls *cs)
492 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
493 return v4l2_g_ext_ctrls_request(hdl, vdev, mdev, cs);
495 return v4l2_g_ext_ctrls_common(hdl, cs, vdev);
497 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
499 /* Validate a new control */
500 static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
505 for (idx = 0; !err && idx < ctrl->new_elems; idx++)
506 err = ctrl->type_ops->validate(ctrl, idx, p_new);
510 /* Validate controls. */
511 static int validate_ctrls(struct v4l2_ext_controls *cs,
512 struct v4l2_ctrl_helper *helpers,
513 struct video_device *vdev,
519 cs->error_idx = cs->count;
520 for (i = 0; i < cs->count; i++) {
521 struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl;
522 union v4l2_ctrl_ptr p_new;
526 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) {
528 "control id 0x%x is read-only\n",
533 * This test is also done in try_set_control_cluster() which
534 * is called in atomic context, so that has the final say,
535 * but it makes sense to do an up-front check as well. Once
536 * an error occurs in try_set_control_cluster() some other
537 * controls may have been set already and we want to do a
538 * best-effort to avoid that.
540 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) {
542 "control id 0x%x is grabbed, cannot set\n",
547 * Skip validation for now if the payload needs to be copied
548 * from userspace into kernelspace. We'll validate those later.
552 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
553 p_new.p_s64 = &cs->controls[i].value64;
555 p_new.p_s32 = &cs->controls[i].value;
556 ret = validate_new(ctrl, p_new);
563 /* Try or try-and-set controls */
564 int try_set_ext_ctrls_common(struct v4l2_fh *fh,
565 struct v4l2_ctrl_handler *hdl,
566 struct v4l2_ext_controls *cs,
567 struct video_device *vdev, bool set)
569 struct v4l2_ctrl_helper helper[4];
570 struct v4l2_ctrl_helper *helpers = helper;
574 cs->error_idx = cs->count;
576 /* Default value cannot be changed */
577 if (cs->which == V4L2_CTRL_WHICH_DEF_VAL) {
578 dprintk(vdev, "%s: cannot change default value\n",
579 video_device_node_name(vdev));
583 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
586 dprintk(vdev, "%s: invalid null control handler\n",
587 video_device_node_name(vdev));
592 return class_check(hdl, cs->which);
594 if (cs->count > ARRAY_SIZE(helper)) {
595 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
600 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, false);
602 ret = validate_ctrls(cs, helpers, vdev, set);
604 cs->error_idx = cs->count;
605 for (i = 0; !ret && i < cs->count; i++) {
606 struct v4l2_ctrl *master;
609 if (!helpers[i].mref)
613 master = helpers[i].mref->ctrl;
614 v4l2_ctrl_lock(master);
616 /* Reset the 'is_new' flags of the cluster */
617 for (j = 0; j < master->ncontrols; j++)
618 if (master->cluster[j])
619 master->cluster[j]->is_new = 0;
622 * For volatile autoclusters that are currently in auto mode
623 * we need to discover if it will be set to manual mode.
624 * If so, then we have to copy the current volatile values
625 * first since those will become the new manual values (which
626 * may be overwritten by explicit new values from this set
629 if (master->is_auto && master->has_volatiles &&
630 !is_cur_manual(master)) {
631 /* Pick an initial non-manual value */
632 s32 new_auto_val = master->manual_mode_value + 1;
637 * Check if the auto control is part of the
638 * list, and remember the new value.
640 if (helpers[tmp_idx].ref->ctrl == master)
641 new_auto_val = cs->controls[tmp_idx].value;
642 tmp_idx = helpers[tmp_idx].next;
645 * If the new value == the manual value, then copy
646 * the current volatile values.
648 if (new_auto_val == master->manual_mode_value)
649 update_from_auto_cluster(master);
653 * Copy the new caller-supplied control values.
654 * user_to_new() sets 'is_new' to 1.
657 struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl;
659 ret = user_to_new(cs->controls + idx, ctrl);
660 if (!ret && ctrl->is_ptr) {
661 ret = validate_new(ctrl, ctrl->p_new);
664 "failed to validate control %s (%d)\n",
665 v4l2_ctrl_get_name(ctrl->id), ret);
667 idx = helpers[idx].next;
668 } while (!ret && idx);
671 ret = try_or_set_cluster(fh, master,
672 !hdl->req_obj.req && set, 0);
673 if (!ret && hdl->req_obj.req && set) {
674 for (j = 0; j < master->ncontrols; j++) {
675 struct v4l2_ctrl_ref *ref =
676 find_ref(hdl, master->cluster[j]->id);
682 /* Copy the new values back to userspace. */
686 ret = new_to_user(cs->controls + idx,
687 helpers[idx].ref->ctrl);
688 idx = helpers[idx].next;
689 } while (!ret && idx);
691 v4l2_ctrl_unlock(master);
694 if (cs->count > ARRAY_SIZE(helper))
699 static int try_set_ext_ctrls(struct v4l2_fh *fh,
700 struct v4l2_ctrl_handler *hdl,
701 struct video_device *vdev,
702 struct media_device *mdev,
703 struct v4l2_ext_controls *cs, bool set)
707 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
708 return try_set_ext_ctrls_request(fh, hdl, vdev, mdev, cs, set);
710 ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
713 "%s: try_set_ext_ctrls_common failed (%d)\n",
714 video_device_node_name(vdev), ret);
719 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl,
720 struct video_device *vdev,
721 struct media_device *mdev,
722 struct v4l2_ext_controls *cs)
724 return try_set_ext_ctrls(NULL, hdl, vdev, mdev, cs, false);
726 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
728 int v4l2_s_ext_ctrls(struct v4l2_fh *fh,
729 struct v4l2_ctrl_handler *hdl,
730 struct video_device *vdev,
731 struct media_device *mdev,
732 struct v4l2_ext_controls *cs)
734 return try_set_ext_ctrls(fh, hdl, vdev, mdev, cs, true);
736 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
739 * VIDIOC_G/S_CTRL implementation
742 /* Helper function to get a single control */
743 static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
745 struct v4l2_ctrl *master = ctrl->cluster[0];
749 /* Compound controls are not supported. The new_to_user() and
750 * cur_to_user() calls below would need to be modified not to access
751 * userspace memory when called from get_ctrl().
753 if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
756 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
759 v4l2_ctrl_lock(master);
760 /* g_volatile_ctrl will update the current control values */
761 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
762 for (i = 0; i < master->ncontrols; i++)
763 cur_to_new(master->cluster[i]);
764 ret = call_op(master, g_volatile_ctrl);
765 new_to_user(c, ctrl);
767 cur_to_user(c, ctrl);
769 v4l2_ctrl_unlock(master);
773 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
775 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
776 struct v4l2_ext_control c;
779 if (!ctrl || !ctrl->is_int)
781 ret = get_ctrl(ctrl, &c);
782 control->value = c.value;
785 EXPORT_SYMBOL(v4l2_g_ctrl);
787 /* Helper function for VIDIOC_S_CTRL compatibility */
788 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
790 struct v4l2_ctrl *master = ctrl->cluster[0];
794 /* Reset the 'is_new' flags of the cluster */
795 for (i = 0; i < master->ncontrols; i++)
796 if (master->cluster[i])
797 master->cluster[i]->is_new = 0;
799 ret = validate_new(ctrl, ctrl->p_new);
804 * For autoclusters with volatiles that are switched from auto to
805 * manual mode we have to update the current volatile values since
806 * those will become the initial manual values after such a switch.
808 if (master->is_auto && master->has_volatiles && ctrl == master &&
809 !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
810 update_from_auto_cluster(master);
813 return try_or_set_cluster(fh, master, true, ch_flags);
816 /* Helper function for VIDIOC_S_CTRL compatibility */
817 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
818 struct v4l2_ext_control *c)
822 v4l2_ctrl_lock(ctrl);
823 user_to_new(c, ctrl);
824 ret = set_ctrl(fh, ctrl, 0);
826 cur_to_user(c, ctrl);
827 v4l2_ctrl_unlock(ctrl);
831 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
832 struct v4l2_control *control)
834 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
835 struct v4l2_ext_control c = { control->id };
838 if (!ctrl || !ctrl->is_int)
841 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
844 c.value = control->value;
845 ret = set_ctrl_lock(fh, ctrl, &c);
846 control->value = c.value;
849 EXPORT_SYMBOL(v4l2_s_ctrl);
852 * Helper functions for drivers to get/set controls.
855 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
857 struct v4l2_ext_control c;
859 /* It's a driver bug if this happens. */
860 if (WARN_ON(!ctrl->is_int))
866 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
868 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
870 struct v4l2_ext_control c;
872 /* It's a driver bug if this happens. */
873 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
879 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
881 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
883 lockdep_assert_held(ctrl->handler->lock);
885 /* It's a driver bug if this happens. */
886 if (WARN_ON(!ctrl->is_int))
889 return set_ctrl(NULL, ctrl, 0);
891 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
893 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
895 lockdep_assert_held(ctrl->handler->lock);
897 /* It's a driver bug if this happens. */
898 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
900 *ctrl->p_new.p_s64 = val;
901 return set_ctrl(NULL, ctrl, 0);
903 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
905 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
907 lockdep_assert_held(ctrl->handler->lock);
909 /* It's a driver bug if this happens. */
910 if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING))
912 strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
913 return set_ctrl(NULL, ctrl, 0);
915 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
917 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
918 enum v4l2_ctrl_type type, const void *p)
920 lockdep_assert_held(ctrl->handler->lock);
922 /* It's a driver bug if this happens. */
923 if (WARN_ON(ctrl->type != type))
925 /* Setting dynamic arrays is not (yet?) supported. */
926 if (WARN_ON(ctrl->is_dyn_array))
928 memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
929 return set_ctrl(NULL, ctrl, 0);
931 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound);
934 * Modify the range of a control.
936 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
937 s64 min, s64 max, u64 step, s64 def)
940 bool range_changed = false;
943 lockdep_assert_held(ctrl->handler->lock);
945 switch (ctrl->type) {
946 case V4L2_CTRL_TYPE_INTEGER:
947 case V4L2_CTRL_TYPE_INTEGER64:
948 case V4L2_CTRL_TYPE_BOOLEAN:
949 case V4L2_CTRL_TYPE_MENU:
950 case V4L2_CTRL_TYPE_INTEGER_MENU:
951 case V4L2_CTRL_TYPE_BITMASK:
952 case V4L2_CTRL_TYPE_U8:
953 case V4L2_CTRL_TYPE_U16:
954 case V4L2_CTRL_TYPE_U32:
957 ret = check_range(ctrl->type, min, max, step, def);
964 if (ctrl->minimum != min || ctrl->maximum != max ||
965 ctrl->step != step || ctrl->default_value != def) {
966 range_changed = true;
970 ctrl->default_value = def;
973 if (validate_new(ctrl, ctrl->p_new)) {
974 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
975 *ctrl->p_new.p_s64 = def;
977 *ctrl->p_new.p_s32 = def;
980 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
981 value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
983 value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
985 ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
986 else if (range_changed)
987 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
990 EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
992 /* Implement VIDIOC_QUERY_EXT_CTRL */
993 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
995 const unsigned int next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
996 u32 id = qc->id & V4L2_CTRL_ID_MASK;
997 struct v4l2_ctrl_ref *ref;
998 struct v4l2_ctrl *ctrl;
1003 mutex_lock(hdl->lock);
1005 /* Try to find it */
1006 ref = find_ref(hdl, id);
1008 if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
1010 /* Match any control that is not hidden */
1011 unsigned int mask = 1;
1014 if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
1015 /* Match any hidden control */
1017 } else if ((qc->id & next_flags) == next_flags) {
1018 /* Match any control, compound or not */
1022 /* Find the next control with ID > qc->id */
1024 /* Did we reach the end of the control list? */
1025 if (id >= node2id(hdl->ctrl_refs.prev)) {
1026 ref = NULL; /* Yes, so there is no next control */
1029 * We found a control with the given ID, so just get
1030 * the next valid one in the list.
1032 list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
1033 is_compound = ref->ctrl->is_array ||
1034 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1035 if (id < ref->ctrl->id &&
1036 (is_compound & mask) == match)
1039 if (&ref->node == &hdl->ctrl_refs)
1043 * No control with the given ID exists, so start
1044 * searching for the next largest ID. We know there
1045 * is one, otherwise the first 'if' above would have
1048 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1049 is_compound = ref->ctrl->is_array ||
1050 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1051 if (id < ref->ctrl->id &&
1052 (is_compound & mask) == match)
1055 if (&ref->node == &hdl->ctrl_refs)
1059 mutex_unlock(hdl->lock);
1065 memset(qc, 0, sizeof(*qc));
1066 if (id >= V4L2_CID_PRIVATE_BASE)
1070 strscpy(qc->name, ctrl->name, sizeof(qc->name));
1071 qc->flags = user_flags(ctrl);
1072 qc->type = ctrl->type;
1073 qc->elem_size = ctrl->elem_size;
1074 qc->elems = ctrl->elems;
1075 qc->nr_of_dims = ctrl->nr_of_dims;
1076 memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
1077 qc->minimum = ctrl->minimum;
1078 qc->maximum = ctrl->maximum;
1079 qc->default_value = ctrl->default_value;
1080 if (ctrl->type == V4L2_CTRL_TYPE_MENU ||
1081 ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1084 qc->step = ctrl->step;
1087 EXPORT_SYMBOL(v4l2_query_ext_ctrl);
1089 /* Implement VIDIOC_QUERYCTRL */
1090 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1092 struct v4l2_query_ext_ctrl qec = { qc->id };
1095 rc = v4l2_query_ext_ctrl(hdl, &qec);
1100 qc->type = qec.type;
1101 qc->flags = qec.flags;
1102 strscpy(qc->name, qec.name, sizeof(qc->name));
1104 case V4L2_CTRL_TYPE_INTEGER:
1105 case V4L2_CTRL_TYPE_BOOLEAN:
1106 case V4L2_CTRL_TYPE_MENU:
1107 case V4L2_CTRL_TYPE_INTEGER_MENU:
1108 case V4L2_CTRL_TYPE_STRING:
1109 case V4L2_CTRL_TYPE_BITMASK:
1110 qc->minimum = qec.minimum;
1111 qc->maximum = qec.maximum;
1112 qc->step = qec.step;
1113 qc->default_value = qec.default_value;
1119 qc->default_value = 0;
1124 EXPORT_SYMBOL(v4l2_queryctrl);
1126 /* Implement VIDIOC_QUERYMENU */
1127 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1129 struct v4l2_ctrl *ctrl;
1132 ctrl = v4l2_ctrl_find(hdl, qm->id);
1138 switch (ctrl->type) {
1139 case V4L2_CTRL_TYPE_MENU:
1143 case V4L2_CTRL_TYPE_INTEGER_MENU:
1144 if (!ctrl->qmenu_int)
1151 if (i < ctrl->minimum || i > ctrl->maximum)
1154 /* Use mask to see if this menu item should be skipped */
1155 if (ctrl->menu_skip_mask & (1ULL << i))
1157 /* Empty menu items should also be skipped */
1158 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1159 if (!ctrl->qmenu[i] || ctrl->qmenu[i][0] == '\0')
1161 strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1163 qm->value = ctrl->qmenu_int[i];
1167 EXPORT_SYMBOL(v4l2_querymenu);
1170 * VIDIOC_LOG_STATUS helpers
1173 int v4l2_ctrl_log_status(struct file *file, void *fh)
1175 struct video_device *vfd = video_devdata(file);
1176 struct v4l2_fh *vfh = file->private_data;
1178 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
1179 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
1180 vfd->v4l2_dev->name);
1183 EXPORT_SYMBOL(v4l2_ctrl_log_status);
1185 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
1187 v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
1190 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
1193 * VIDIOC_(UN)SUBSCRIBE_EVENT implementation
1196 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev,
1199 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1204 v4l2_ctrl_lock(ctrl);
1205 list_add_tail(&sev->node, &ctrl->ev_subs);
1206 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
1207 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL))
1208 send_initial_event(sev->fh, ctrl);
1209 v4l2_ctrl_unlock(ctrl);
1213 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
1215 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1220 v4l2_ctrl_lock(ctrl);
1221 list_del(&sev->node);
1222 v4l2_ctrl_unlock(ctrl);
1225 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
1227 u32 old_changes = old->u.ctrl.changes;
1229 old->u.ctrl = new->u.ctrl;
1230 old->u.ctrl.changes |= old_changes;
1232 EXPORT_SYMBOL(v4l2_ctrl_replace);
1234 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
1236 new->u.ctrl.changes |= old->u.ctrl.changes;
1238 EXPORT_SYMBOL(v4l2_ctrl_merge);
1240 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
1241 .add = v4l2_ctrl_add_event,
1242 .del = v4l2_ctrl_del_event,
1243 .replace = v4l2_ctrl_replace,
1244 .merge = v4l2_ctrl_merge,
1246 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
1248 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
1249 const struct v4l2_event_subscription *sub)
1251 if (sub->type == V4L2_EVENT_CTRL)
1252 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
1255 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
1257 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1258 struct v4l2_event_subscription *sub)
1260 if (!sd->ctrl_handler)
1262 return v4l2_ctrl_subscribe_event(fh, sub);
1264 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
1269 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
1271 struct v4l2_fh *fh = file->private_data;
1273 poll_wait(file, &fh->wait, wait);
1274 if (v4l2_event_pending(fh))
1278 EXPORT_SYMBOL(v4l2_ctrl_poll);