From patchwork Tue Sep 6 11:50:34 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 1126112 X-Patchwork-Delegate: jikos@jikos.cz Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p86BpCg4020984 for ; Tue, 6 Sep 2011 11:51:22 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754251Ab1IFLvW (ORCPT ); Tue, 6 Sep 2011 07:51:22 -0400 Received: from mail-fx0-f46.google.com ([209.85.161.46]:44212 "EHLO mail-fx0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754239Ab1IFLvV (ORCPT ); Tue, 6 Sep 2011 07:51:21 -0400 Received: by mail-fx0-f46.google.com with SMTP id 19so4508571fxh.19 for ; Tue, 06 Sep 2011 04:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=8GrzwOPgOSVmEle1ah+anOR9tBMI/a/jAEFIUsWInjM=; b=hX/Br2DWeX9p47SFRsJSnMHV+Q9Xfjbf9ZDfpQ+UGeVTEMLWjcQtnbSUI1TfVv30EH MoDCfsWoeBoygQk67snxfR5ReEpE7znnes65fneMQ09n4vWKVBGNGe6p4FZVNjv35Hnl rOUh6p23AGe4BKXHxUlBvS5R43PhrfzkS6TWQ= Received: by 10.223.47.75 with SMTP id m11mr1169670faf.55.1315309881245; Tue, 06 Sep 2011 04:51:21 -0700 (PDT) Received: from localhost.localdomain (stgt-5f708ff4.pool.mediaWays.net [95.112.143.244]) by mx.google.com with ESMTPS id q23sm5071202fae.1.2011.09.06.04.51.19 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 06 Sep 2011 04:51:20 -0700 (PDT) From: David Herrmann To: linux-input@vger.kernel.org Cc: jkosina@suse.cz, David Herrmann Subject: [PATCH v2 09/15] HID: wiimote: Helper functions for synchronous requests Date: Tue, 6 Sep 2011 13:50:34 +0200 Message-Id: <1315309840-4664-10-git-send-email-dh.herrmann@googlemail.com> X-Mailer: git-send-email 1.7.6.1 In-Reply-To: <1315309840-4664-1-git-send-email-dh.herrmann@googlemail.com> References: <1315309840-4664-1-git-send-email-dh.herrmann@googlemail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@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]); Tue, 06 Sep 2011 11:51:22 +0000 (UTC) To initialize wiimote peripherals, the stream to the wiimote must be held exclusively by the initializer, otherwise the initialization will fail. Many initializations require multiple memory requests to be sent synchronously so we need a way to lock the stream and release it when we are done. This adds several helper functions which allow to lock the stream, then send requests, wait for the answers and release the stream again. When holding the lock, the function may sleep and interrupted by signals. Also it returns after a short timeout so userspace shouldn't notice long delays. Signed-off-by: David Herrmann --- drivers/hid/hid-wiimote.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-) diff --git a/drivers/hid/hid-wiimote.c b/drivers/hid/hid-wiimote.c index c811a7d..0a5e458 100644 --- a/drivers/hid/hid-wiimote.c +++ b/drivers/hid/hid-wiimote.c @@ -10,11 +10,13 @@ * any later version. */ +#include #include #include #include #include #include +#include #include #include "hid-ids.h" @@ -31,6 +33,12 @@ struct wiimote_state { spinlock_t lock; __u8 flags; __u8 accel_split[2]; + + /* synchronous cmd requests */ + struct mutex sync; + struct completion ready; + int cmd; + __u32 opt; }; struct wiimote_data { @@ -118,6 +126,52 @@ static __u16 wiiproto_keymap[] = { BTN_MODE, /* WIIPROTO_KEY_HOME */ }; +/* requires the state.lock spinlock to be held */ +static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, + __u32 opt) +{ + return wdata->state.cmd == cmd && wdata->state.opt == opt; +} + +/* requires the state.lock spinlock to be held */ +static inline void wiimote_cmd_complete(struct wiimote_data *wdata) +{ + wdata->state.cmd = WIIPROTO_REQ_NULL; + complete(&wdata->state.ready); +} + +static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) +{ + return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; +} + +/* requires the state.lock spinlock to be held */ +static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, + __u32 opt) +{ + INIT_COMPLETION(wdata->state.ready); + wdata->state.cmd = cmd; + wdata->state.opt = opt; +} + +static inline void wiimote_cmd_release(struct wiimote_data *wdata) +{ + mutex_unlock(&wdata->state.sync); +} + +static inline int wiimote_cmd_wait(struct wiimote_data *wdata) +{ + int ret; + + ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); + if (ret < 0) + return -ERESTARTSYS; + else if (ret == 0) + return -EIO; + else + return 0; +} + static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, size_t count) { @@ -875,6 +929,8 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev) INIT_WORK(&wdata->worker, wiimote_worker); spin_lock_init(&wdata->state.lock); + init_completion(&wdata->state.ready); + mutex_init(&wdata->state.sync); return wdata;