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);
757 ret = new_to_user(c, ctrl);
759 ret = cur_to_user(c, ctrl);
761 v4l2_ctrl_unlock(master);
765 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
767 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
768 struct v4l2_ext_control c;
771 if (!ctrl || !ctrl->is_int)
773 ret = get_ctrl(ctrl, &c);
776 control->value = c.value;
780 EXPORT_SYMBOL(v4l2_g_ctrl);
782 /* Helper function for VIDIOC_S_CTRL compatibility */
783 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
785 struct v4l2_ctrl *master = ctrl->cluster[0];
789 /* Reset the 'is_new' flags of the cluster */
790 for (i = 0; i < master->ncontrols; i++)
791 if (master->cluster[i])
792 master->cluster[i]->is_new = 0;
794 ret = validate_new(ctrl, ctrl->p_new);
799 * For autoclusters with volatiles that are switched from auto to
800 * manual mode we have to update the current volatile values since
801 * those will become the initial manual values after such a switch.
803 if (master->is_auto && master->has_volatiles && ctrl == master &&
804 !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
805 update_from_auto_cluster(master);
808 return try_or_set_cluster(fh, master, true, ch_flags);
811 /* Helper function for VIDIOC_S_CTRL compatibility */
812 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
813 struct v4l2_ext_control *c)
817 v4l2_ctrl_lock(ctrl);
818 ret = user_to_new(c, ctrl);
820 ret = set_ctrl(fh, ctrl, 0);
822 ret = cur_to_user(c, ctrl);
823 v4l2_ctrl_unlock(ctrl);
827 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
828 struct v4l2_control *control)
830 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
831 struct v4l2_ext_control c = { control->id };
834 if (!ctrl || !ctrl->is_int)
837 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
840 c.value = control->value;
841 ret = set_ctrl_lock(fh, ctrl, &c);
842 control->value = c.value;
845 EXPORT_SYMBOL(v4l2_s_ctrl);
848 * Helper functions for drivers to get/set controls.
851 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
853 struct v4l2_ext_control c;
855 /* It's a driver bug if this happens. */
856 if (WARN_ON(!ctrl->is_int))
862 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
864 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
866 struct v4l2_ext_control c;
868 /* It's a driver bug if this happens. */
869 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
875 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
877 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
879 lockdep_assert_held(ctrl->handler->lock);
881 /* It's a driver bug if this happens. */
882 if (WARN_ON(!ctrl->is_int))
885 return set_ctrl(NULL, ctrl, 0);
887 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
889 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
891 lockdep_assert_held(ctrl->handler->lock);
893 /* It's a driver bug if this happens. */
894 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
896 *ctrl->p_new.p_s64 = val;
897 return set_ctrl(NULL, ctrl, 0);
899 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
901 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
903 lockdep_assert_held(ctrl->handler->lock);
905 /* It's a driver bug if this happens. */
906 if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING))
908 strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
909 return set_ctrl(NULL, ctrl, 0);
911 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
913 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
914 enum v4l2_ctrl_type type, const void *p)
916 lockdep_assert_held(ctrl->handler->lock);
918 /* It's a driver bug if this happens. */
919 if (WARN_ON(ctrl->type != type))
921 /* Setting dynamic arrays is not (yet?) supported. */
922 if (WARN_ON(ctrl->is_dyn_array))
924 memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
925 return set_ctrl(NULL, ctrl, 0);
927 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound);
930 * Modify the range of a control.
932 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
933 s64 min, s64 max, u64 step, s64 def)
936 bool range_changed = false;
939 lockdep_assert_held(ctrl->handler->lock);
941 switch (ctrl->type) {
942 case V4L2_CTRL_TYPE_INTEGER:
943 case V4L2_CTRL_TYPE_INTEGER64:
944 case V4L2_CTRL_TYPE_BOOLEAN:
945 case V4L2_CTRL_TYPE_MENU:
946 case V4L2_CTRL_TYPE_INTEGER_MENU:
947 case V4L2_CTRL_TYPE_BITMASK:
948 case V4L2_CTRL_TYPE_U8:
949 case V4L2_CTRL_TYPE_U16:
950 case V4L2_CTRL_TYPE_U32:
953 ret = check_range(ctrl->type, min, max, step, def);
960 if (ctrl->minimum != min || ctrl->maximum != max ||
961 ctrl->step != step || ctrl->default_value != def) {
962 range_changed = true;
966 ctrl->default_value = def;
969 if (validate_new(ctrl, ctrl->p_new)) {
970 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
971 *ctrl->p_new.p_s64 = def;
973 *ctrl->p_new.p_s32 = def;
976 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
977 value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
979 value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
981 ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
982 else if (range_changed)
983 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
986 EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
988 int __v4l2_ctrl_modify_dimensions(struct v4l2_ctrl *ctrl,
989 u32 dims[V4L2_CTRL_MAX_DIMS])
991 unsigned int elems = 1;
995 lockdep_assert_held(ctrl->handler->lock);
997 if (!ctrl->is_array || ctrl->is_dyn_array)
1000 for (i = 0; i < ctrl->nr_of_dims; i++)
1004 p_array = kvzalloc(2 * elems * ctrl->elem_size, GFP_KERNEL);
1007 kvfree(ctrl->p_array);
1008 ctrl->p_array_alloc_elems = elems;
1009 ctrl->elems = elems;
1010 ctrl->new_elems = elems;
1011 ctrl->p_array = p_array;
1012 ctrl->p_new.p = p_array;
1013 ctrl->p_cur.p = p_array + elems * ctrl->elem_size;
1014 for (i = 0; i < ctrl->nr_of_dims; i++)
1015 ctrl->dims[i] = dims[i];
1016 ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
1018 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_VALUE |
1019 V4L2_EVENT_CTRL_CH_DIMENSIONS);
1022 EXPORT_SYMBOL(__v4l2_ctrl_modify_dimensions);
1024 /* Implement VIDIOC_QUERY_EXT_CTRL */
1025 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
1027 const unsigned int next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
1028 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1029 struct v4l2_ctrl_ref *ref;
1030 struct v4l2_ctrl *ctrl;
1035 mutex_lock(hdl->lock);
1037 /* Try to find it */
1038 ref = find_ref(hdl, id);
1040 if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
1042 /* Match any control that is not hidden */
1043 unsigned int mask = 1;
1046 if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
1047 /* Match any hidden control */
1049 } else if ((qc->id & next_flags) == next_flags) {
1050 /* Match any control, compound or not */
1054 /* Find the next control with ID > qc->id */
1056 /* Did we reach the end of the control list? */
1057 if (id >= node2id(hdl->ctrl_refs.prev)) {
1058 ref = NULL; /* Yes, so there is no next control */
1060 struct v4l2_ctrl_ref *pos = ref;
1063 * We found a control with the given ID, so just get
1064 * the next valid one in the list.
1067 list_for_each_entry_continue(pos, &hdl->ctrl_refs, node) {
1068 is_compound = pos->ctrl->is_array ||
1069 pos->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1070 if (id < pos->ctrl->id &&
1071 (is_compound & mask) == match) {
1077 struct v4l2_ctrl_ref *pos;
1080 * No control with the given ID exists, so start
1081 * searching for the next largest ID. We know there
1082 * is one, otherwise the first 'if' above would have
1085 list_for_each_entry(pos, &hdl->ctrl_refs, node) {
1086 is_compound = pos->ctrl->is_array ||
1087 pos->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
1088 if (id < pos->ctrl->id &&
1089 (is_compound & mask) == match) {
1096 mutex_unlock(hdl->lock);
1102 memset(qc, 0, sizeof(*qc));
1103 if (id >= V4L2_CID_PRIVATE_BASE)
1107 strscpy(qc->name, ctrl->name, sizeof(qc->name));
1108 qc->flags = user_flags(ctrl);
1109 qc->type = ctrl->type;
1110 qc->elem_size = ctrl->elem_size;
1111 qc->elems = ctrl->elems;
1112 qc->nr_of_dims = ctrl->nr_of_dims;
1113 memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
1114 qc->minimum = ctrl->minimum;
1115 qc->maximum = ctrl->maximum;
1116 qc->default_value = ctrl->default_value;
1117 if (ctrl->type == V4L2_CTRL_TYPE_MENU ||
1118 ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1121 qc->step = ctrl->step;
1124 EXPORT_SYMBOL(v4l2_query_ext_ctrl);
1126 /* Implement VIDIOC_QUERYCTRL */
1127 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1129 struct v4l2_query_ext_ctrl qec = { qc->id };
1132 rc = v4l2_query_ext_ctrl(hdl, &qec);
1137 qc->type = qec.type;
1138 qc->flags = qec.flags;
1139 strscpy(qc->name, qec.name, sizeof(qc->name));
1141 case V4L2_CTRL_TYPE_INTEGER:
1142 case V4L2_CTRL_TYPE_BOOLEAN:
1143 case V4L2_CTRL_TYPE_MENU:
1144 case V4L2_CTRL_TYPE_INTEGER_MENU:
1145 case V4L2_CTRL_TYPE_STRING:
1146 case V4L2_CTRL_TYPE_BITMASK:
1147 qc->minimum = qec.minimum;
1148 qc->maximum = qec.maximum;
1149 qc->step = qec.step;
1150 qc->default_value = qec.default_value;
1156 qc->default_value = 0;
1161 EXPORT_SYMBOL(v4l2_queryctrl);
1163 /* Implement VIDIOC_QUERYMENU */
1164 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1166 struct v4l2_ctrl *ctrl;
1169 ctrl = v4l2_ctrl_find(hdl, qm->id);
1175 switch (ctrl->type) {
1176 case V4L2_CTRL_TYPE_MENU:
1180 case V4L2_CTRL_TYPE_INTEGER_MENU:
1181 if (!ctrl->qmenu_int)
1188 if (i < ctrl->minimum || i > ctrl->maximum)
1191 /* Use mask to see if this menu item should be skipped */
1192 if (i < BITS_PER_LONG_LONG && (ctrl->menu_skip_mask & BIT_ULL(i)))
1194 /* Empty menu items should also be skipped */
1195 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1196 if (!ctrl->qmenu[i] || ctrl->qmenu[i][0] == '\0')
1198 strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1200 qm->value = ctrl->qmenu_int[i];
1204 EXPORT_SYMBOL(v4l2_querymenu);
1207 * VIDIOC_LOG_STATUS helpers
1210 int v4l2_ctrl_log_status(struct file *file, void *fh)
1212 struct video_device *vfd = video_devdata(file);
1213 struct v4l2_fh *vfh = file->private_data;
1215 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
1216 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
1217 vfd->v4l2_dev->name);
1220 EXPORT_SYMBOL(v4l2_ctrl_log_status);
1222 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
1224 v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
1227 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
1230 * VIDIOC_(UN)SUBSCRIBE_EVENT implementation
1233 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev,
1236 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1241 v4l2_ctrl_lock(ctrl);
1242 list_add_tail(&sev->node, &ctrl->ev_subs);
1243 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
1244 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL))
1245 send_initial_event(sev->fh, ctrl);
1246 v4l2_ctrl_unlock(ctrl);
1250 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
1252 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
1257 v4l2_ctrl_lock(ctrl);
1258 list_del(&sev->node);
1259 v4l2_ctrl_unlock(ctrl);
1262 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
1264 u32 old_changes = old->u.ctrl.changes;
1266 old->u.ctrl = new->u.ctrl;
1267 old->u.ctrl.changes |= old_changes;
1269 EXPORT_SYMBOL(v4l2_ctrl_replace);
1271 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
1273 new->u.ctrl.changes |= old->u.ctrl.changes;
1275 EXPORT_SYMBOL(v4l2_ctrl_merge);
1277 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
1278 .add = v4l2_ctrl_add_event,
1279 .del = v4l2_ctrl_del_event,
1280 .replace = v4l2_ctrl_replace,
1281 .merge = v4l2_ctrl_merge,
1283 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
1285 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
1286 const struct v4l2_event_subscription *sub)
1288 if (sub->type == V4L2_EVENT_CTRL)
1289 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
1292 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
1294 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1295 struct v4l2_event_subscription *sub)
1297 if (!sd->ctrl_handler)
1299 return v4l2_ctrl_subscribe_event(fh, sub);
1301 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
1306 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
1308 struct v4l2_fh *fh = file->private_data;
1310 poll_wait(file, &fh->wait, wait);
1311 if (v4l2_event_pending(fh))
1315 EXPORT_SYMBOL(v4l2_ctrl_poll);