[media] v4l2-events/fh: merge v4l2_events into v4l2_fh
[pandora-kernel.git] / drivers / media / video / v4l2-ctrls.c
index 98b1b8d..a532561 100644 (file)
@@ -23,6 +23,7 @@
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
+#include <media/v4l2-event.h>
 #include <media/v4l2-dev.h>
 
 #define has_op(master, op) \
@@ -39,6 +40,20 @@ struct ctrl_helper {
        bool handled;
 };
 
+/* Small helper function to determine if the autocluster is set to manual
+   mode. In that case the is_volatile flag should be ignored. */
+static bool is_cur_manual(const struct v4l2_ctrl *master)
+{
+       return master->is_auto && master->cur.val == master->manual_mode_value;
+}
+
+/* Same as above, but this checks the against the new value instead of the
+   current value. */
+static bool is_new_manual(const struct v4l2_ctrl *master)
+{
+       return master->is_auto && master->val == master->manual_mode_value;
+}
+
 /* Returns NULL or a character pointer array containing the menu for
    the given control ID. The pointer array ends with a NULL pointer.
    An empty string signifies a menu entry that is invalid. This allows
@@ -542,6 +557,41 @@ static bool type_is_int(const struct v4l2_ctrl *ctrl)
        }
 }
 
+static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
+{
+       memset(ev->reserved, 0, sizeof(ev->reserved));
+       ev->type = V4L2_EVENT_CTRL;
+       ev->id = ctrl->id;
+       ev->u.ctrl.changes = changes;
+       ev->u.ctrl.type = ctrl->type;
+       ev->u.ctrl.flags = ctrl->flags;
+       if (ctrl->type == V4L2_CTRL_TYPE_STRING)
+               ev->u.ctrl.value64 = 0;
+       else
+               ev->u.ctrl.value64 = ctrl->cur.val64;
+       ev->u.ctrl.minimum = ctrl->minimum;
+       ev->u.ctrl.maximum = ctrl->maximum;
+       if (ctrl->type == V4L2_CTRL_TYPE_MENU)
+               ev->u.ctrl.step = 1;
+       else
+               ev->u.ctrl.step = ctrl->step;
+       ev->u.ctrl.default_value = ctrl->default_value;
+}
+
+static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
+{
+       struct v4l2_event ev;
+       struct v4l2_ctrl_fh *pos;
+
+       if (list_empty(&ctrl->fhs))
+                       return;
+       fill_event(&ev, ctrl, changes);
+
+       list_for_each_entry(pos, &ctrl->fhs, node)
+               if (pos->fh != fh)
+                       v4l2_event_queue_fh(pos->fh, &ev);
+}
+
 /* Helper function: copy the current control value back to the caller */
 static int cur_to_user(struct v4l2_ext_control *c,
                       struct v4l2_ctrl *ctrl)
@@ -643,22 +693,40 @@ static int ctrl_is_volatile(struct v4l2_ext_control *c,
 }
 
 /* Copy the new value to the current value. */
-static void new_to_cur(struct v4l2_ctrl *ctrl)
+static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
+                                               bool update_inactive)
 {
+       bool changed = false;
+
        if (ctrl == NULL)
                return;
        switch (ctrl->type) {
+       case V4L2_CTRL_TYPE_BUTTON:
+               changed = true;
+               break;
        case V4L2_CTRL_TYPE_STRING:
                /* strings are always 0-terminated */
+               changed = strcmp(ctrl->string, ctrl->cur.string);
                strcpy(ctrl->cur.string, ctrl->string);
                break;
        case V4L2_CTRL_TYPE_INTEGER64:
+               changed = ctrl->val64 != ctrl->cur.val64;
                ctrl->cur.val64 = ctrl->val64;
                break;
        default:
+               changed = ctrl->val != ctrl->cur.val;
                ctrl->cur.val = ctrl->val;
                break;
        }
+       if (update_inactive) {
+               ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
+               if (!is_cur_manual(ctrl->cluster[0]))
+                       ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
+       }
+       if (changed || update_inactive)
+               send_event(fh, ctrl,
+                       (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
+                       (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
 }
 
 /* Copy the current value to the new value */
@@ -799,6 +867,7 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 {
        struct v4l2_ctrl_ref *ref, *next_ref;
        struct v4l2_ctrl *ctrl, *next_ctrl;
+       struct v4l2_ctrl_fh *ctrl_fh, *next_ctrl_fh;
 
        if (hdl == NULL || hdl->buckets == NULL)
                return;
@@ -812,6 +881,10 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
        /* Free all controls owned by the handler */
        list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
                list_del(&ctrl->node);
+               list_for_each_entry_safe(ctrl_fh, next_ctrl_fh, &ctrl->fhs, node) {
+                       list_del(&ctrl_fh->node);
+                       kfree(ctrl_fh);
+               }
                kfree(ctrl);
        }
        kfree(hdl->buckets);
@@ -940,6 +1013,7 @@ static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
           insertion is an O(1) operation. */
        if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
                list_add_tail(&new_ref->node, &hdl->ctrl_refs);
+               hdl->nr_of_refs++;
                goto insert_in_hash;
        }
 
@@ -1010,6 +1084,7 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
        }
 
        INIT_LIST_HEAD(&ctrl->node);
+       INIT_LIST_HEAD(&ctrl->fhs);
        ctrl->handler = hdl;
        ctrl->ops = ops;
        ctrl->id = id;
@@ -1151,6 +1226,9 @@ int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
                /* Skip handler-private controls. */
                if (ctrl->is_private)
                        continue;
+               /* And control classes */
+               if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
+                       continue;
                ret = handler_new_ref(hdl, ctrl);
                if (ret)
                        break;
@@ -1166,7 +1244,7 @@ void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
        int i;
 
        /* The first control is the master control and it must not be NULL */
-       BUG_ON(controls[0] == NULL);
+       BUG_ON(ncontrols == 0 || controls[0] == NULL);
 
        for (i = 0; i < ncontrols; i++) {
                if (controls[i]) {
@@ -1177,18 +1255,47 @@ void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
 }
 EXPORT_SYMBOL(v4l2_ctrl_cluster);
 
+void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
+                           u8 manual_val, bool set_volatile)
+{
+       struct v4l2_ctrl *master = controls[0];
+       u32 flag;
+       int i;
+
+       v4l2_ctrl_cluster(ncontrols, controls);
+       WARN_ON(ncontrols <= 1);
+       WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
+       master->is_auto = true;
+       master->manual_mode_value = manual_val;
+       master->flags |= V4L2_CTRL_FLAG_UPDATE;
+       flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
+
+       for (i = 1; i < ncontrols; i++)
+               if (controls[i]) {
+                       controls[i]->is_volatile = set_volatile;
+                       controls[i]->flags |= flag;
+               }
+}
+EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
+
 /* Activate/deactivate a control. */
 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
 {
+       /* invert since the actual flag is called 'inactive' */
+       bool inactive = !active;
+       bool old;
+
        if (ctrl == NULL)
                return;
 
-       if (!active)
+       if (inactive)
                /* set V4L2_CTRL_FLAG_INACTIVE */
-               set_bit(4, &ctrl->flags);
+               old = test_and_set_bit(4, &ctrl->flags);
        else
                /* clear V4L2_CTRL_FLAG_INACTIVE */
-               clear_bit(4, &ctrl->flags);
+               old = test_and_clear_bit(4, &ctrl->flags);
+       if (old != inactive)
+               send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
 }
 EXPORT_SYMBOL(v4l2_ctrl_activate);
 
@@ -1200,15 +1307,21 @@ EXPORT_SYMBOL(v4l2_ctrl_activate);
    these controls. */
 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
 {
+       bool old;
+
        if (ctrl == NULL)
                return;
 
+       v4l2_ctrl_lock(ctrl);
        if (grabbed)
                /* set V4L2_CTRL_FLAG_GRABBED */
-               set_bit(1, &ctrl->flags);
+               old = test_and_set_bit(1, &ctrl->flags);
        else
                /* clear V4L2_CTRL_FLAG_GRABBED */
-               clear_bit(1, &ctrl->flags);
+               old = test_and_clear_bit(1, &ctrl->flags);
+       if (old != grabbed)
+               send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
+       v4l2_ctrl_unlock(ctrl);
 }
 EXPORT_SYMBOL(v4l2_ctrl_grab);
 
@@ -1595,7 +1708,7 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs
                v4l2_ctrl_lock(master);
 
                /* g_volatile_ctrl will update the new control values */
-               if (has_volatiles) {
+               if (has_volatiles && !is_cur_manual(master)) {
                        for (j = 0; j < master->ncontrols; j++)
                                cur_to_new(master->cluster[j]);
                        ret = call_op(master, g_volatile_ctrl);
@@ -1633,7 +1746,7 @@ static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
 
        v4l2_ctrl_lock(master);
        /* g_volatile_ctrl will update the current control values */
-       if (ctrl->is_volatile) {
+       if (ctrl->is_volatile && !is_cur_manual(master)) {
                for (i = 0; i < master->ncontrols; i++)
                        cur_to_new(master->cluster[i]);
                ret = call_op(master, g_volatile_ctrl);
@@ -1676,8 +1789,10 @@ EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
 /* Core function that calls try/s_ctrl and ensures that the new value is
    copied to the current value on a set.
    Must be called with ctrl->handler->lock held. */
-static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
+static int try_or_set_control_cluster(struct v4l2_fh *fh,
+                                       struct v4l2_ctrl *master, bool set)
 {
+       bool update_flag;
        bool try = !set;
        int ret = 0;
        int i;
@@ -1717,18 +1832,22 @@ static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
                ret = call_op(master, try_ctrl);
 
        /* Don't set if there is no change */
-       if (!ret && set && cluster_changed(master)) {
-               ret = call_op(master, s_ctrl);
-               /* If OK, then make the new values permanent. */
-               if (!ret)
-                       for (i = 0; i < master->ncontrols; i++)
-                               new_to_cur(master->cluster[i]);
-       }
-       return ret;
+       if (ret || !set || !cluster_changed(master))
+               return ret;
+       ret = call_op(master, s_ctrl);
+       /* If OK, then make the new values permanent. */
+       if (ret)
+               return ret;
+
+       update_flag = is_cur_manual(master) != is_new_manual(master);
+       for (i = 0; i < master->ncontrols; i++)
+               new_to_cur(fh, master->cluster[i], update_flag && i > 0);
+       return 0;
 }
 
 /* Try or set controls. */
-static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
+static int try_or_set_ext_ctrls(struct v4l2_fh *fh,
+                               struct v4l2_ctrl_handler *hdl,
                                struct v4l2_ext_controls *cs,
                                struct ctrl_helper *helpers,
                                bool set)
@@ -1773,7 +1892,7 @@ static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
                ret = cluster_walk(i, cs, helpers, user_to_new);
 
                if (!ret)
-                       ret = try_or_set_control_cluster(master, set);
+                       ret = try_or_set_control_cluster(fh, master, set);
 
                /* Copy the new values back to userspace. */
                if (!ret)
@@ -1786,7 +1905,7 @@ static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
 }
 
 /* Try or try-and-set controls */
-static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
+static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
                             struct v4l2_ext_controls *cs,
                             bool set)
 {
@@ -1813,7 +1932,7 @@ static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
 
        /* First 'try' all controls and abort on error */
        if (!ret)
-               ret = try_or_set_ext_ctrls(hdl, cs, helpers, false);
+               ret = try_or_set_ext_ctrls(NULL, hdl, cs, helpers, false);
        /* If this is a 'set' operation and the initial 'try' failed,
           then set error_idx to count to tell the application that no
           controls changed value yet. */
@@ -1823,7 +1942,7 @@ static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
                /* Reset 'handled' state */
                for (i = 0; i < cs->count; i++)
                        helpers[i].handled = false;
-               ret = try_or_set_ext_ctrls(hdl, cs, helpers, true);
+               ret = try_or_set_ext_ctrls(fh, hdl, cs, helpers, true);
        }
 
        if (cs->count > ARRAY_SIZE(helper))
@@ -1833,30 +1952,31 @@ static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
 
 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
 {
-       return try_set_ext_ctrls(hdl, cs, false);
+       return try_set_ext_ctrls(NULL, hdl, cs, false);
 }
 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
 
-int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
+int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
+                                       struct v4l2_ext_controls *cs)
 {
-       return try_set_ext_ctrls(hdl, cs, true);
+       return try_set_ext_ctrls(fh, hdl, cs, true);
 }
 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
 
 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
 {
-       return try_set_ext_ctrls(sd->ctrl_handler, cs, false);
+       return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
 }
 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
 
 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
 {
-       return try_set_ext_ctrls(sd->ctrl_handler, cs, true);
+       return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
 }
 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
 
 /* Helper function for VIDIOC_S_CTRL compatibility */
-static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
+static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
 {
        struct v4l2_ctrl *master = ctrl->cluster[0];
        int ret;
@@ -1871,15 +1991,16 @@ static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
 
        ctrl->val = *val;
        ctrl->is_new = 1;
-       ret = try_or_set_control_cluster(master, false);
+       ret = try_or_set_control_cluster(NULL, master, false);
        if (!ret)
-               ret = try_or_set_control_cluster(master, true);
+               ret = try_or_set_control_cluster(fh, master, true);
        *val = ctrl->cur.val;
        v4l2_ctrl_unlock(ctrl);
        return ret;
 }
 
-int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
+int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
+                                       struct v4l2_control *control)
 {
        struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
 
@@ -1889,13 +2010,13 @@ int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
        if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
                return -EACCES;
 
-       return set_ctrl(ctrl, &control->value);
+       return set_ctrl(fh, ctrl, &control->value);
 }
 EXPORT_SYMBOL(v4l2_s_ctrl);
 
 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
 {
-       return v4l2_s_ctrl(sd->ctrl_handler, control);
+       return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
 }
 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
 
@@ -1903,6 +2024,59 @@ int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
 {
        /* It's a driver bug if this happens. */
        WARN_ON(!type_is_int(ctrl));
-       return set_ctrl(ctrl, &val);
+       return set_ctrl(NULL, ctrl, &val);
 }
 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
+
+void v4l2_ctrl_add_fh(struct v4l2_ctrl_handler *hdl,
+               struct v4l2_ctrl_fh *ctrl_fh,
+               struct v4l2_event_subscription *sub)
+{
+       struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, sub->id);
+
+       v4l2_ctrl_lock(ctrl);
+       list_add_tail(&ctrl_fh->node, &ctrl->fhs);
+       if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
+           (sub->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
+               struct v4l2_event ev;
+
+               fill_event(&ev, ctrl, V4L2_EVENT_CTRL_CH_VALUE |
+                       V4L2_EVENT_CTRL_CH_FLAGS);
+               v4l2_event_queue_fh(ctrl_fh->fh, &ev);
+       }
+       v4l2_ctrl_unlock(ctrl);
+}
+EXPORT_SYMBOL(v4l2_ctrl_add_fh);
+
+void v4l2_ctrl_del_fh(struct v4l2_ctrl *ctrl, struct v4l2_fh *fh)
+{
+       struct v4l2_ctrl_fh *pos;
+
+       v4l2_ctrl_lock(ctrl);
+       list_for_each_entry(pos, &ctrl->fhs, node) {
+               if (pos->fh == fh) {
+                       list_del(&pos->node);
+                       kfree(pos);
+                       break;
+               }
+       }
+       v4l2_ctrl_unlock(ctrl);
+}
+EXPORT_SYMBOL(v4l2_ctrl_del_fh);
+
+int v4l2_ctrl_subscribe_fh(struct v4l2_fh *fh,
+                       struct v4l2_event_subscription *sub, unsigned n)
+{
+       struct v4l2_ctrl_handler *hdl = fh->ctrl_handler;
+       int ret = 0;
+
+       if (!ret) {
+               if (hdl->nr_of_refs * 2 > n)
+                       n = hdl->nr_of_refs * 2;
+               ret = v4l2_event_alloc(fh, n);
+       }
+       if (!ret)
+               ret = v4l2_event_subscribe(fh, sub);
+       return ret;
+}
+EXPORT_SYMBOL(v4l2_ctrl_subscribe_fh);