Skip to content

Commit 8ac7a94

Browse files
Hans VerkuilMauro Carvalho Chehab
authored andcommitted
[media] v4l2-ctrls: add a notify callback
Sometimes platform/bridge drivers need to be notified when a control from a sub-device changes value. In order to support this a notify callback was added. [dheitmueller@kernellabs.com: fix merge conflict in v4l2-ctrls.c] Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Devin Heitmueller <dheitmueller@kernellabs.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
1 parent 20deebf commit 8ac7a94

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

‎Documentation/video4linux/v4l2-controls.txt‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,14 +715,20 @@ a control of this type whenever the first control belonging to a new control
715715
class is added.
716716

717717

718-
Proposals for Extensions
719-
========================
718+
Adding Notify Callbacks
719+
=======================
720+
721+
Sometimes the platform or bridge driver needs to be notified when a control
722+
from a sub-device driver changes. You can set a notify callback by calling
723+
this function:
720724

721-
Some ideas for future extensions to the spec:
725+
void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl,
726+
void (*notify)(struct v4l2_ctrl *ctrl, void *priv), void *priv);
722727

723-
1) Add a V4L2_CTRL_FLAG_HEX to have values shown as hexadecimal instead of
724-
decimal. Useful for e.g. video_mute_yuv.
728+
Whenever the give control changes value the notify callback will be called
729+
with a pointer to the control and the priv pointer that was passed with
730+
v4l2_ctrl_notify. Note that the control's handler lock is held when the
731+
notify function is called.
725732

726-
2) It is possible to mark in the controls array which controls have been
727-
successfully written and which failed by for example adding a bit to the
728-
control ID. Not sure if it is worth the effort, though.
733+
There can be only one notify function per control handler. Any attempt
734+
to set another notify function will cause a WARN_ON.

‎drivers/media/v4l2-core/v4l2-ctrls.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,8 @@ static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
12041204
send_event(fh, ctrl,
12051205
(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
12061206
(update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
1207+
if (ctrl->call_notify && changed && ctrl->handler->notify)
1208+
ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
12071209
}
12081210
}
12091211

@@ -2725,6 +2727,22 @@ int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
27252727
}
27262728
EXPORT_SYMBOL(v4l2_ctrl_s_ctrl_int64);
27272729

2730+
void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
2731+
{
2732+
if (ctrl == NULL)
2733+
return;
2734+
if (notify == NULL) {
2735+
ctrl->call_notify = 0;
2736+
return;
2737+
}
2738+
if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
2739+
return;
2740+
ctrl->handler->notify = notify;
2741+
ctrl->handler->notify_priv = priv;
2742+
ctrl->call_notify = 1;
2743+
}
2744+
EXPORT_SYMBOL(v4l2_ctrl_notify);
2745+
27282746
static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
27292747
{
27302748
struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);

‎include/media/v4l2-ctrls.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ struct v4l2_ctrl_ops {
5353
int (*s_ctrl)(struct v4l2_ctrl *ctrl);
5454
};
5555

56+
typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
57+
5658
/** struct v4l2_ctrl - The control structure.
5759
* @node: The list node.
5860
* @ev_subs: The list of control event subscriptions.
@@ -72,6 +74,8 @@ struct v4l2_ctrl_ops {
7274
* set this flag directly.
7375
* @has_volatiles: If set, then one or more members of the cluster are volatile.
7476
* Drivers should never touch this flag.
77+
* @call_notify: If set, then call the handler's notify function whenever the
78+
* control's value changes.
7579
* @manual_mode_value: If the is_auto flag is set, then this is the value
7680
* of the auto control that determines if that control is in
7781
* manual mode. So if the value of the auto control equals this
@@ -119,6 +123,7 @@ struct v4l2_ctrl {
119123
unsigned int is_private:1;
120124
unsigned int is_auto:1;
121125
unsigned int has_volatiles:1;
126+
unsigned int call_notify:1;
122127
unsigned int manual_mode_value:8;
123128

124129
const struct v4l2_ctrl_ops *ops;
@@ -177,6 +182,10 @@ struct v4l2_ctrl_ref {
177182
* control is needed multiple times, so this is a simple
178183
* optimization.
179184
* @buckets: Buckets for the hashing. Allows for quick control lookup.
185+
* @notify: A notify callback that is called whenever the control changes value.
186+
* Note that the handler's lock is held when the notify function
187+
* is called!
188+
* @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
180189
* @nr_of_buckets: Total number of buckets in the array.
181190
* @error: The error code of the first failed control addition.
182191
*/
@@ -187,6 +196,8 @@ struct v4l2_ctrl_handler {
187196
struct list_head ctrl_refs;
188197
struct v4l2_ctrl_ref *cached;
189198
struct v4l2_ctrl_ref **buckets;
199+
v4l2_ctrl_notify_fnc notify;
200+
void *notify_priv;
190201
u16 nr_of_buckets;
191202
int error;
192203
};
@@ -525,6 +536,20 @@ static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
525536
mutex_unlock(ctrl->handler->lock);
526537
}
527538

539+
/** v4l2_ctrl_notify() - Function to set a notify callback for a control.
540+
* @ctrl: The control.
541+
* @notify: The callback function.
542+
* @priv: The callback private handle, passed as argument to the callback.
543+
*
544+
* This function sets a callback function for the control. If @ctrl is NULL,
545+
* then it will do nothing. If @notify is NULL, then the notify callback will
546+
* be removed.
547+
*
548+
* There can be only one notify. If another already exists, then a WARN_ON
549+
* will be issued and the function will do nothing.
550+
*/
551+
void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv);
552+
528553
/** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
529554
* @ctrl: The control.
530555
*

0 commit comments

Comments
 (0)