From patchwork Wed May 25 13:33:52 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Verkuil X-Patchwork-Id: 816232 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p4PDYTJA023377 for ; Wed, 25 May 2011 13:34:30 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932699Ab1EYNe1 (ORCPT ); Wed, 25 May 2011 09:34:27 -0400 Received: from smtp-vbr5.xs4all.nl ([194.109.24.25]:3185 "EHLO smtp-vbr5.xs4all.nl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932569Ab1EYNeS (ORCPT ); Wed, 25 May 2011 09:34:18 -0400 Received: from tschai (64-103-25-233.cisco.com [64.103.25.233]) (authenticated bits=0) by smtp-vbr5.xs4all.nl (8.13.8/8.13.8) with ESMTP id p4PDYARc007307 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 25 May 2011 15:34:15 +0200 (CEST) (envelope-from hverkuil@xs4all.nl) From: Hans Verkuil To: linux-media@vger.kernel.org Cc: Hans Verkuil Subject: [RFCv2 PATCH 08/11] v4l2-ctrls: simplify event subscription. Date: Wed, 25 May 2011 15:33:52 +0200 Message-Id: <2993c04b0ba330b3f634e281a6b50ee8cd7e6f7c.1306329390.git.hans.verkuil@cisco.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1306330435-11799-1-git-send-email-hverkuil@xs4all.nl> References: <1306330435-11799-1-git-send-email-hverkuil@xs4all.nl> In-Reply-To: <6cea502820c1684f34b9e862a64be2972afb718f.1306329390.git.hans.verkuil@cisco.com> References: <6cea502820c1684f34b9e862a64be2972afb718f.1306329390.git.hans.verkuil@cisco.com> X-Virus-Scanned: by XS4ALL Virus Scanner Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Wed, 25 May 2011 13:34:30 +0000 (UTC) From: Hans Verkuil Signed-off-by: Hans Verkuil --- drivers/media/video/v4l2-ctrls.c | 31 +++++++++++++++++++++++++++++++ include/media/v4l2-ctrls.h | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 0 deletions(-) diff --git a/drivers/media/video/v4l2-ctrls.c b/drivers/media/video/v4l2-ctrls.c index e2a7ac7..9807a20 100644 --- a/drivers/media/video/v4l2-ctrls.c +++ b/drivers/media/video/v4l2-ctrls.c @@ -831,6 +831,22 @@ int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl, } EXPORT_SYMBOL(v4l2_ctrl_handler_init); +/* Count the number of controls */ +unsigned v4l2_ctrl_handler_cnt(struct v4l2_ctrl_handler *hdl) +{ + struct v4l2_ctrl_ref *ref; + unsigned cnt = 0; + + if (hdl == NULL) + return 0; + mutex_lock(&hdl->lock); + list_for_each_entry(ref, &hdl->ctrl_refs, node) + cnt++; + mutex_unlock(&hdl->lock); + return cnt; +} +EXPORT_SYMBOL(v4l2_ctrl_handler_cnt); + /* Free all controls and control refs */ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) { @@ -1999,3 +2015,18 @@ void v4l2_ctrl_del_fh(struct v4l2_ctrl *ctrl, struct v4l2_fh *fh) v4l2_ctrl_unlock(ctrl); } EXPORT_SYMBOL(v4l2_ctrl_del_fh); + +int v4l2_ctrl_sub_fh(struct v4l2_fh *fh, struct v4l2_event_subscription *sub, + unsigned n) +{ + int ret = 0; + + if (!fh->events) + ret = v4l2_event_init(fh); + if (!ret) + ret = v4l2_event_alloc(fh, n); + if (!ret) + ret = v4l2_event_subscribe(fh, sub); + return ret; +} +EXPORT_SYMBOL(v4l2_ctrl_sub_fh); diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h index 2b99f29..e2b9053 100644 --- a/include/media/v4l2-ctrls.h +++ b/include/media/v4l2-ctrls.h @@ -252,6 +252,14 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl, unsigned nr_of_controls_hint); +/** v4l2_ctrl_handler_cnt() - Count the number of controls in the handler. + * @hdl: The control handler. + * + * Returns the number of controls referenced by this handler. + * Returns 0 if @hdl == NULL. + */ +unsigned v4l2_ctrl_handler_cnt(struct v4l2_ctrl_handler *hdl); + /** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free * the control list. * @hdl: The control handler. @@ -446,11 +454,28 @@ s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl); */ int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val); +/* Internal helper functions that deal with control events. */ void v4l2_ctrl_add_fh(struct v4l2_ctrl_handler *hdl, struct v4l2_ctrl_fh *ctrl_fh, struct v4l2_event_subscription *sub); void v4l2_ctrl_del_fh(struct v4l2_ctrl *ctrl, struct v4l2_fh *fh); +/** v4l2_ctrl_sub_fh() - Helper function that subscribes a control event. + * @fh: The file handler that subscribed the control event. + * @sub: The event to subscribe (type must be V4L2_EVENT_CTRL). + * @n: How many events should be allocated? (Passed to v4l2_event_alloc). + * Recommended to set to twice the number of controls plus whatever + * is needed for other events. + * + * A helper function that initializes the fh for events, allocates the + * list of events and subscribes the control event. + * + * Typically called in the handler of VIDIOC_SUBSCRIBE_EVENT in the + * V4L2_EVENT_CTRL case. + */ +int v4l2_ctrl_sub_fh(struct v4l2_fh *fh, struct v4l2_event_subscription *sub, + unsigned n); + /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */ int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc); int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);