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)
92 ctrl->type_ops->init(ctrl, 0, ctrl->p_new);
94 return ptr_to_user(c, ctrl, ctrl->p_new);
97 /* Helper function: copy the caller-provider value as the new control value */
98 static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
104 if (ctrl->is_dyn_array &&
105 c->size > ctrl->p_array_alloc_elems * ctrl->elem_size) {
106 void *old = ctrl->p_array;
107 void *tmp = kvzalloc(2 * c->size, GFP_KERNEL);
111 memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
112 memcpy(tmp + c->size, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
114 ctrl->p_cur.p = tmp + c->size;
116 ctrl->p_array_alloc_elems = c->size / ctrl->elem_size;
120 if (ctrl->is_ptr && !ctrl->is_string) {
121 unsigned int elems = c->size / ctrl->elem_size;
123 if (copy_from_user(ctrl->p_new.p, c->ptr, c->size))
126 if (ctrl->is_dyn_array)
127 ctrl->new_elems = elems;
128 else if (ctrl->is_array)
129 ctrl->type_ops->init(ctrl, elems, ctrl->p_new);
133 switch (ctrl->type) {
134 case V4L2_CTRL_TYPE_INTEGER64:
135 *ctrl->p_new.p_s64 = c->value64;
137 case V4L2_CTRL_TYPE_STRING:
141 if (size > ctrl->maximum + 1)
142 size = ctrl->maximum + 1;
143 ret = copy_from_user(ctrl->p_new.p_char, c->string, size) ? -EFAULT : 0;
145 char last = ctrl->p_new.p_char[size - 1];
147 ctrl->p_new.p_char[size - 1] = 0;
149 * If the string was longer than ctrl->maximum,
150 * then return an error.
152 if (strlen(ctrl->p_new.p_char) == ctrl->maximum && last)
158 *ctrl->p_new.p_s32 = c->value;
166 * VIDIOC_G/TRY/S_EXT_CTRLS implementation
170 * Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
172 * It is not a fully atomic operation, just best-effort only. After all, if
173 * multiple controls have to be set through multiple i2c writes (for example)
174 * then some initial writes may succeed while others fail. Thus leaving the
175 * system in an inconsistent state. The question is how much effort you are
176 * willing to spend on trying to make something atomic that really isn't.
178 * From the point of view of an application the main requirement is that
179 * when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
180 * error should be returned without actually affecting any controls.
182 * If all the values are correct, then it is acceptable to just give up
183 * in case of low-level errors.
185 * It is important though that the application can tell when only a partial
186 * configuration was done. The way we do that is through the error_idx field
187 * of struct v4l2_ext_controls: if that is equal to the count field then no
188 * controls were affected. Otherwise all controls before that index were
189 * successful in performing their 'get' or 'set' operation, the control at
190 * the given index failed, and you don't know what happened with the controls
191 * after the failed one. Since if they were part of a control cluster they
192 * could have been successfully processed (if a cluster member was encountered
193 * at index < error_idx), they could have failed (if a cluster member was at
194 * error_idx), or they may not have been processed yet (if the first cluster
195 * member appeared after error_idx).
197 * It is all fairly theoretical, though. In practice all you can do is to
198 * bail out. If error_idx == count, then it is an application bug. If
199 * error_idx < count then it is only an application bug if the error code was
200 * EBUSY. That usually means that something started streaming just when you
201 * tried to set the controls. In all other cases it is a driver/hardware
202 * problem and all you can do is to retry or bail out.
204 * Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
205 * never modifies controls the error_idx is just set to whatever control
206 * has an invalid value.
210 * Prepare for the extended g/s/try functions.
211 * Find the controls in the control array and do some basic checks.
213 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
214 struct v4l2_ext_controls *cs,
215 struct v4l2_ctrl_helper *helpers,
216 struct video_device *vdev,
219 struct v4l2_ctrl_helper *h;
220 bool have_clusters = false;
223 for (i = 0, h = helpers; i < cs->count; i++, h++) {
224 struct v4l2_ext_control *c = &cs->controls[i];
225 struct v4l2_ctrl_ref *ref;
226 struct v4l2_ctrl *ctrl;
227 u32 id = c->id & V4L2_CTRL_ID_MASK;
232 cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
233 cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
234 V4L2_CTRL_ID2WHICH(id) != cs->which) {
236 "invalid which 0x%x or control id 0x%x\n",
242 * Old-style private controls are not allowed for
245 if (id >= V4L2_CID_PRIVATE_BASE) {
247 "old-style private controls not allowed\n");
250 ref = find_ref_lock(hdl, id);
252 dprintk(vdev, "cannot find control id 0x%x\n", id);
257 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
258 dprintk(vdev, "control id 0x%x is disabled\n", id);
262 if (ctrl->cluster[0]->ncontrols > 1)
263 have_clusters = true;
264 if (ctrl->cluster[0] != ctrl)
265 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
266 if (ctrl->is_dyn_array) {
267 unsigned int max_size = ctrl->dims[0] * ctrl->elem_size;
268 unsigned int tot_size = ctrl->elem_size;
270 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
271 tot_size *= ref->p_req_elems;
273 tot_size *= ctrl->elems;
275 c->size = ctrl->elem_size * (c->size / ctrl->elem_size);
277 if (c->size < tot_size) {
283 if (c->size > max_size) {
290 } else if (ctrl->is_ptr && !ctrl->is_string) {
291 unsigned int tot_size = ctrl->elems * ctrl->elem_size;
293 if (c->size < tot_size) {
295 * In the get case the application first
296 * queries to obtain the size of the control.
303 "pointer control id 0x%x size too small, %d bytes but %d bytes needed\n",
304 id, c->size, tot_size);
309 /* Store the ref to the master control of the cluster */
312 * Initially set next to 0, meaning that there is no other
313 * control in this helper array belonging to the same
320 * We are done if there were no controls that belong to a multi-
327 * The code below figures out in O(n) time which controls in the list
328 * belong to the same cluster.
331 /* This has to be done with the handler lock taken. */
332 mutex_lock(hdl->lock);
334 /* First zero the helper field in the master control references */
335 for (i = 0; i < cs->count; i++)
336 helpers[i].mref->helper = NULL;
337 for (i = 0, h = helpers; i < cs->count; i++, h++) {
338 struct v4l2_ctrl_ref *mref = h->mref;
341 * If the mref->helper is set, then it points to an earlier
342 * helper that belongs to the same cluster.
346 * Set the next field of mref->helper to the current
347 * index: this means that the earlier helper now
348 * points to the next helper in the same cluster.
350 mref->helper->next = i;
352 * mref should be set only for the first helper in the
353 * cluster, clear the others.
357 /* Point the mref helper to the current helper struct. */
360 mutex_unlock(hdl->lock);
365 * Handles the corner case where cs->count == 0. It checks whether the
366 * specified control class exists. If that class ID is 0, then it checks
367 * whether there are any controls at all.
369 static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
371 if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL ||
372 which == V4L2_CTRL_WHICH_REQUEST_VAL)
374 return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
378 * Get extended controls. Allocates the helpers array if needed.
380 * Note that v4l2_g_ext_ctrls_common() with 'which' set to
381 * V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was
382 * completed, and in that case p_req_valid is true for all controls.
384 int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
385 struct v4l2_ext_controls *cs,
386 struct video_device *vdev)
388 struct v4l2_ctrl_helper helper[4];
389 struct v4l2_ctrl_helper *helpers = helper;
392 bool is_default, is_request;
394 is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
395 is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL);
397 cs->error_idx = cs->count;
398 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
404 return class_check(hdl, cs->which);
406 if (cs->count > ARRAY_SIZE(helper)) {
407 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
413 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, true);
414 cs->error_idx = cs->count;
416 for (i = 0; !ret && i < cs->count; i++)
417 if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
420 for (i = 0; !ret && i < cs->count; i++) {
421 struct v4l2_ctrl *master;
422 bool is_volatile = false;
425 if (!helpers[i].mref)
428 master = helpers[i].mref->ctrl;
431 v4l2_ctrl_lock(master);
434 * g_volatile_ctrl will update the new control values.
435 * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL and
436 * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests
437 * it is v4l2_ctrl_request_complete() that copies the
438 * volatile controls at the time of request completion
439 * to the request, so you don't want to do that again.
441 if (!is_default && !is_request &&
442 ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
443 (master->has_volatiles && !is_cur_manual(master)))) {
444 for (j = 0; j < master->ncontrols; j++)
445 cur_to_new(master->cluster[j]);
446 ret = call_op(master, g_volatile_ctrl);
451 v4l2_ctrl_unlock(master);
456 * Copy the default value (if is_default is true), the
457 * request value (if is_request is true and p_req is valid),
458 * the new volatile value (if is_volatile is true) or the
462 struct v4l2_ctrl_ref *ref = helpers[idx].ref;
465 ret = def_to_user(cs->controls + idx, ref->ctrl);
466 else if (is_request && ref->p_req_array_enomem)
468 else if (is_request && ref->p_req_valid)
469 ret = req_to_user(cs->controls + idx, ref);
470 else if (is_volatile)
471 ret = new_to_user(cs->controls + idx, ref->ctrl);
473 ret = cur_to_user(cs->controls + idx, ref->ctrl);
474 idx = helpers[idx].next;
475 } while (!ret && idx);
477 v4l2_ctrl_unlock(master);
480 if (cs->count > ARRAY_SIZE(helper))
485 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
486 struct media_device *mdev, struct v4l2_ext_controls *cs)
488 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
489 return v4l2_g_ext_ctrls_request(hdl, vdev, mdev, cs);
491 return v4l2_g_ext_ctrls_common(hdl, cs, vdev);
493 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
495 /* Validate a new control */
496 static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
498 return ctrl->type_ops->validate(ctrl, p_new);
501 /* Validate controls. */
502 static int validate_ctrls(struct v4l2_ext_controls *cs,
503 struct v4l2_ctrl_helper *helpers,
504 struct video_device *vdev,
510 cs->error_idx = cs->count;
511 for (i = 0; i < cs->count; i++) {
512 struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl;
513 union v4l2_ctrl_ptr p_new;
517 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) {
519 "control id 0x%x is read-only\n",
524 * This test is also done in try_set_control_cluster() which
525 * is called in atomic context, so that has the final say,
526 * but it makes sense to do an up-front check as well. Once
527 * an error occurs in try_set_control_cluster() some other
528 * controls may have been set already and we want to do a
529 * best-effort to avoid that.
531 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) {
533 "control id 0x%x is grabbed, cannot set\n",
538 * Skip validation for now if the payload needs to be copied
539 * from userspace into kernelspace. We'll validate those later.
543 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
544 p_new.p_s64 = &cs->controls[i].value64;
546 p_new.p_s32 = &cs->controls[i].value;
547 ret = validate_new(ctrl, p_new);
554 /* Try or try-and-set controls */
555 int try_set_ext_ctrls_common(struct v4l2_fh *fh,
556 struct v4l2_ctrl_handler *hdl,
557 struct v4l2_ext_controls *cs,
558 struct video_device *vdev, bool set)
560 struct v4l2_ctrl_helper helper[4];
561 struct v4l2_ctrl_helper *helpers = helper;
565 cs->error_idx = cs->count;
567 /* Default value cannot be changed */
568 if (cs->which == V4L2_CTRL_WHICH_DEF_VAL) {
569 dprintk(vdev, "%s: cannot change default value\n",
570 video_device_node_name(vdev));
574 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
577 dprintk(vdev, "%s: invalid null control handler\n",
578 video_device_node_name(vdev));
583 return class_check(hdl, cs->which);
585 if (cs->count > ARRAY_SIZE(helper)) {
586 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
591 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, false);
593 ret = validate_ctrls(cs, helpers, vdev, set);
595 cs->error_idx = cs->count;
596 for (i = 0; !ret && i < cs->count; i++) {
597 struct v4l2_ctrl *master;
600 if (!helpers[i].mref)
604 master = helpers[i].mref->ctrl;
605 v4l2_ctrl_lock(master);
607 /* Reset the 'is_new' flags of the cluster */
608 for (j = 0; j < master->ncontrols; j++)
609 if (master->cluster[j])
610 master->cluster[j]->is_new = 0;
613 * For volatile autoclusters that are currently in auto mode
614 * we need to discover if it will be set to manual mode.
615 * If so, then we have to copy the current volatile values
616 * first since those will become the new manual values (which
617 * may be overwritten by explicit new values from this set
620 if (master->is_auto && master->has_volatiles &&
621 !is_cur_manual(master)) {
622 /* Pick an initial non-manual value */
623 s32 new_auto_val = master->manual_mode_value + 1;
628 * Check if the auto control is part of the
629 * list, and remember the new value.
631 if (helpers[tmp_idx].ref->ctrl == master)
632 new_auto_val = cs->controls[tmp_idx].value;
633 tmp_idx = helpers[tmp_idx].next;
636 * If the new value == the manual value, then copy
637 * the current volatile values.
639 if (new_auto_val == master->manual_mode_value)
640 update_from_auto_cluster(master);
644 * Copy the new caller-supplied control values.
645 * user_to_new() sets 'is_new' to 1.
648 struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl;
650 ret = user_to_new(cs->controls + idx, ctrl);
651 if (!ret && ctrl->is_ptr) {
652 ret = validate_new(ctrl, ctrl->p_new);
655 "failed to validate control %s (%d)\n",
656 v4l2_ctrl_get_name(ctrl->id), ret);
658 idx = helpers[idx].next;
659 } while (!ret && idx);
662 ret = try_or_set_cluster(fh, master,
663 !hdl->req_obj.req && set, 0);
664 if (!ret && hdl->req_obj.req && set) {
665 for (j = 0; j < master->ncontrols; j++) {
666 struct v4l2_ctrl_ref *ref =
667 find_ref(hdl, master->cluster[j]->id);
673 /* Copy the new values back to userspace. */
677 ret = new_to_user(cs->controls + idx,
678 helpers[idx].ref->ctrl);
679 idx = helpers[idx].next;
680 } while (!ret && idx);
682 v4l2_ctrl_unlock(master);
685 if (cs->count > ARRAY_SIZE(helper))
690 static int try_set_ext_ctrls(struct v4l2_fh *fh,
691 struct v4l2_ctrl_handler *hdl,
692 struct video_device *vdev,
693 struct media_device *mdev,
694 struct v4l2_ext_controls *cs, bool set)
698 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
699 return try_set_ext_ctrls_request(fh, hdl, vdev, mdev, cs, set);
701 ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
704 "%s: try_set_ext_ctrls_common failed (%d)\n",
705 video_device_node_name(vdev), ret);
710 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl,
711 struct video_device *vdev,
712 struct media_device *mdev,
713 struct v4l2_ext_controls *cs)
715 return try_set_ext_ctrls(NULL, hdl, vdev, mdev, cs, false);
717 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
719 int v4l2_s_ext_ctrls(struct v4l2_fh *fh,
720 struct v4l2_ctrl_handler *hdl,
721 struct video_device *vdev,
722 struct media_device *mdev,
723 struct v4l2_ext_controls *cs)
725 return try_set_ext_ctrls(fh, hdl, vdev, mdev, cs, true);
727 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
730 * VIDIOC_G/S_CTRL implementation
733 /* Helper function to get a single control */
734 static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
736 struct v4l2_ctrl *master = ctrl->cluster[0];
740 /* Compound controls are not supported. The new_to_user() and
741 * cur_to_user() calls below would need to be modified not to access
742 * userspace memory when called from get_ctrl().
744 if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
747 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
750 v4l2_ctrl_lock(master);
751 /* g_volatile_ctrl will update the current control values */
752 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
753 for (i = 0; i < master->ncontrols; i++)
754 cur_to_new(master->cluster[i]);
755 ret = call_op(master, g_volatile_ctrl);
756 new_to_user(c, ctrl);
758 cur_to_user(c, ctrl);
760 v4l2_ctrl_unlock(master);
764 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
766 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
767 struct v4l2_ext_control c;
770 if (!ctrl || !ctrl->is_int)
772 ret = get_ctrl(ctrl, &c);
773 control->value = c.value;
776 EXPORT_SYMBOL(v4l2_g_ctrl);
778 /* Helper function for VIDIOC_S_CTRL compatibility */
779 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
781 struct v4l2_ctrl *master = ctrl->cluster[0];
785 /* Reset the 'is_new' flags of the cluster */
786 for (i = 0; i < master->ncontrols; i++)
787 if (master->cluster[i])
788 master->cluster[i]->is_new = 0;
790 ret = validate_new(ctrl, ctrl->p_new);
795 * For autoclusters with volatiles that are switched from auto to
796 * manual mode we have to update the current volatile values since
797 * those will become the initial manual values after such a switch.
799 if (master->is_auto && master->has_volatiles && ctrl == master &&
800 !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
801 update_from_auto_cluster(master);
804 return try_or_set_cluster(fh, master, true, ch_flags);
807 /* Helper function for VIDIOC_S_CTRL compatibility */
808 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
809 struct v4l2_ext_control *c)
813 v4l2_ctrl_lock(ctrl);
814 user_to_new(c, ctrl);
815 ret = set_ctrl(fh, ctrl, 0);
817 cur_to_user(c, ctrl);
818 v4l2_ctrl_unlock(ctrl);
822 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
823 struct v4l2_control *control)
825 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
826 struct v4l2_ext_control c = { control->id };
829 if (!ctrl || !ctrl->is_int)
832 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
835 c.value = control->value;
836 ret = set_ctrl_lock(fh, ctrl, &c);
837 control->value = c.value;
840 EXPORT_SYMBOL(v4l2_s_ctrl);
843 * Helper functions for drivers to get/set controls.
846 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
848 struct v4l2_ext_control c;
850 /* It's a driver bug if this happens. */
851 if (WARN_ON(!ctrl->is_int))
857 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
859 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
861 struct v4l2_ext_control c;
863 /* It's a driver bug if this happens. */
864 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
870 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
872 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
874 lockdep_assert_held(ctrl->handler->lock);
876 /* It's a driver bug if this happens. */
877 if (WARN_ON(!ctrl->is_int))
880 return set_ctrl(NULL, ctrl, 0);
882 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
884 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
886 lockdep_assert_held(ctrl->handler->lock);
888 /* It's a driver bug if this happens. */
889 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
891 *ctrl->p_new.p_s64 = val;
892 return set_ctrl(NULL, ctrl, 0);
894 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
896 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
898 lockdep_assert_held(ctrl->handler->lock);
900 /* It's a driver bug if this happens. */
901 if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING))
903 strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
904 return set_ctrl(NULL, ctrl, 0);
906 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
908 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
909 enum v4l2_ctrl_type type, const void *p)
911 lockdep_assert_held(ctrl->handler->lock);
913 /* It's a driver bug if this happens. */
914 if (WARN_ON(ctrl->type != type))
916 /* Setting dynamic arrays is not (yet?) supported. */
917 if (WARN_ON(ctrl->is_dyn_array))
919 memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
920 return set_ctrl(NULL, ctrl, 0);
922 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound);
925 * Modify the range of a control.
927 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
928 s64 min, s64 max, u64 step, s64 def)
931 bool range_changed = false;
934 lockdep_assert_held(ctrl->handler->lock);
936 switch (ctrl->type) {
937 case V4L2_CTRL_TYPE_INTEGER:
938 case V4L2_CTRL_TYPE_INTEGER64:
939 case V4L2_CTRL_TYPE_BOOLEAN:
940 case V4L2_CTRL_TYPE_MENU:
941 case V4L2_CTRL_TYPE_INTEGER_MENU:
942 case V4L2_CTRL_TYPE_BITMASK:
943 case V4L2_CTRL_TYPE_U8:
944 case V4L2_CTRL_TYPE_U16:
945 case V4L2_CTRL_TYPE_U32:
948 ret = check_range(ctrl->type, min, max, step, def);
955 if (ctrl->minimum != min || ctrl->maximum != max ||
956 ctrl->step != step || ctrl->default_value != def) {
957 range_changed = true;
961 ctrl->default_value = def;
964 if (validate_new(ctrl, ctrl->p_new)) {
965 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
966 *ctrl->p_new.p_s64 = def;
968 *ctrl->p_new.p_s32 = def;
971 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
972 value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
974 value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
976 ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
977 else if (range_changed)
978 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
981 EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
983 int __v4l2_ctrl_modify_dimensions(struct v4l2_ctrl *ctrl,
984 u32 dims[V4L2_CTRL_MAX_DIMS])
986 unsigned int elems = 1;
990 lockdep_assert_held(ctrl->handler->lock);
992 if (!ctrl->is_array || ctrl->is_dyn_array)
995 for (i = 0; i < ctrl->nr_of_dims; i++)
999 p_array = kvzalloc(2 * elems * ctrl->elem_size, GFP_KERNEL);
1002 kvfree(ctrl->p_array);
1003 ctrl->p_array_alloc_elems = elems;
1004 ctrl->elems = elems;
1005 ctrl->new_elems = elems;
1006 ctrl->p_array = p_array;
1007 ctrl->p_new.p = p_array;
1008 ctrl->p_cur.p = p_array + elems * ctrl->elem_size;
1009 for (i = 0; i < ctrl->nr_of_dims; i++)
1010 ctrl->dims[i] = dims[i];
1011 ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
1013 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_VALUE |
1014 V4L2_EVENT_CTRL_CH_DIMENSIONS);
1017 EXPORT_SYMBOL(__v4l2_ctrl_modify_dimensions);
1019 /* Implement VIDIOC_QUERY_EXT_CTRL */
1020 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
1022 const unsigned int next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
1023 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1024 struct v4l2_ctrl_ref *ref;
1025 struct v4l2_ctrl *ctrl;
1030 mutex_lock(hdl->lock);
1032 /* Try to find it */
1033 ref = find_ref(hdl, id);
1035 if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
1037 /* Match any control that is not hidden */
1038 unsigned int mask = 1;
1041 if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
1042 /* Match any hidden control */
1044 } else if ((qc->id & next_flags) == next_flags) {
1045 /* Match any control, compound or not */
1049 /* Find the next control with ID > qc->id */
1051 /* Did we reach the end of the control list? */
1052 if (id >= node2id(hdl->ctrl_refs.prev)) {
1053 ref = NULL; /* Yes, so there is no next control */
1056 * We found a control with the given ID, so just get
1057 * the next valid one in the list.
1059 list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
1060 is_compound = ref->ctrl->is_array ||
1061 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1062 if (id < ref->ctrl->id &&
1063 (is_compound & mask) == match)
1066 if (&ref->node == &hdl->ctrl_refs)
1070 * No control with the given ID exists, so start
1071 * searching for the next largest ID. We know there
1072 * is one, otherwise the first 'if' above would have
1075 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1076 is_compound = ref->ctrl->is_array ||
1077 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1078 if (id < ref->ctrl->id &&
1079 (is_compound & mask) == match)
1082 if (&ref->node == &hdl->ctrl_refs)
1086 mutex_unlock(hdl->lock);
1092 memset(qc, 0, sizeof(*qc));
1093 if (id >= V4L2_CID_PRIVATE_BASE)
1097 strscpy(qc->name, ctrl->name, sizeof(qc->name));
1098 qc->flags = user_flags(ctrl);
1099 qc->type = ctrl->type;
1100 qc->elem_size = ctrl->elem_size;
1101 qc->elems = ctrl->elems;
1102 qc->nr_of_dims = ctrl->nr_of_dims;
1103 memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
1104 qc->minimum = ctrl->minimum;
1105 qc->maximum = ctrl->maximum;
1106 qc->default_value = ctrl->default_value;
1107 if (ctrl->type == V4L2_CTRL_TYPE_MENU ||
1108 ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1111 qc->step = ctrl->step;
1114 EXPORT_SYMBOL(v4l2_query_ext_ctrl);
1116 /* Implement VIDIOC_QUERYCTRL */
1117 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1119 struct v4l2_query_ext_ctrl qec = { qc->id };
1122 rc = v4l2_query_ext_ctrl(hdl, &qec);
1127 qc->type = qec.type;
1128 qc->flags = qec.flags;
1129 strscpy(qc->name, qec.name, sizeof(qc->name));
1131 case V4L2_CTRL_TYPE_INTEGER:
1132 case V4L2_CTRL_TYPE_BOOLEAN:
1133 case V4L2_CTRL_TYPE_MENU:
1134 case V4L2_CTRL_TYPE_INTEGER_MENU:
1135 case V4L2_CTRL_TYPE_STRING:
1136 case V4L2_CTRL_TYPE_BITMASK:
1137 qc->minimum = qec.minimum;
1138 qc->maximum = qec.maximum;
1139 qc->step = qec.step;
1140 qc->default_value = qec.default_value;
1146 qc->default_value = 0;
1151 EXPORT_SYMBOL(v4l2_queryctrl);
1153 /* Implement VIDIOC_QUERYMENU */
1154 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1156 struct v4l2_ctrl *ctrl;
1159 ctrl = v4l2_ctrl_find(hdl, qm->id);
1165 switch (ctrl->type) {
1166 case V4L2_CTRL_TYPE_MENU:
1170 case V4L2_CTRL_TYPE_INTEGER_MENU:
1171 if (!ctrl->qmenu_int)
1178 if (i < ctrl->minimum || i > ctrl->maximum)
1181 /* Use mask to see if this menu item should be skipped */
1182 if (ctrl->menu_skip_mask & (1ULL << i))
1184 /* Empty menu items should also be skipped */
1185 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1186 if (!ctrl->qmenu[i] || ctrl->qmenu[i][0] == '\0')
1188 strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1190 qm->value = ctrl->qmenu_int[i];
1194 EXPORT_SYMBOL(v4l2_querymenu);
1197 * VIDIOC_LOG_STATUS helpers
1200 int v4l2_ctrl_log_status(struct file *file, void *fh)
1202 struct video_device *vfd = video_devdata(file);
1203 struct v4l2_fh *vfh = file->private_data;
1205 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
1206 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
1207 vfd->v4l2_dev->name);
1210 EXPORT_SYMBOL(v4l2_ctrl_log_status);
1212 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
1214 v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
1217 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
1220 * VIDIOC_(UN)SUBSCRIBE_EVENT implementation
1223 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev,
1226 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1231 v4l2_ctrl_lock(ctrl);
1232 list_add_tail(&sev->node, &ctrl->ev_subs);
1233 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
1234 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL))
1235 send_initial_event(sev->fh, ctrl);
1236 v4l2_ctrl_unlock(ctrl);
1240 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
1242 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1247 v4l2_ctrl_lock(ctrl);
1248 list_del(&sev->node);
1249 v4l2_ctrl_unlock(ctrl);
1252 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
1254 u32 old_changes = old->u.ctrl.changes;
1256 old->u.ctrl = new->u.ctrl;
1257 old->u.ctrl.changes |= old_changes;
1259 EXPORT_SYMBOL(v4l2_ctrl_replace);
1261 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
1263 new->u.ctrl.changes |= old->u.ctrl.changes;
1265 EXPORT_SYMBOL(v4l2_ctrl_merge);
1267 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
1268 .add = v4l2_ctrl_add_event,
1269 .del = v4l2_ctrl_del_event,
1270 .replace = v4l2_ctrl_replace,
1271 .merge = v4l2_ctrl_merge,
1273 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
1275 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
1276 const struct v4l2_event_subscription *sub)
1278 if (sub->type == V4L2_EVENT_CTRL)
1279 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
1282 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
1284 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1285 struct v4l2_event_subscription *sub)
1287 if (!sd->ctrl_handler)
1289 return v4l2_ctrl_subscribe_event(fh, sub);
1291 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
1296 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
1298 struct v4l2_fh *fh = file->private_data;
1300 poll_wait(file, &fh->wait, wait);
1301 if (v4l2_event_pending(fh))
1305 EXPORT_SYMBOL(v4l2_ctrl_poll);