From patchwork Tue Sep 6 11:50:39 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 1126162 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 p86BpCg9020984 for ; Tue, 6 Sep 2011 11:51:29 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754303Ab1IFLv2 (ORCPT ); Tue, 6 Sep 2011 07:51:28 -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 S1754296Ab1IFLv2 (ORCPT ); Tue, 6 Sep 2011 07:51:28 -0400 Received: by mail-fx0-f46.google.com with SMTP id 19so4508571fxh.19 for ; Tue, 06 Sep 2011 04:51:27 -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=5br7FDjOs+9fU+2NGj5eHKzGnvGvXp8ropV/5E7TKhE=; b=C3ESMDHREPbhEnQQvkvuRHc1cu8xiX6KR1WjOF404SU2FXL9PKHgUiG/Pp4lPvVVVQ AaR1Sak/FKS4PU+GPZj1/AffhtveCEAlv9eyG/DZKF0r5VSiDRKtpzqACJLiXT2LIYT5 hTBmeHLHUkouJk1xSeXYFRtm7Mu2sw1xUsDt4= Received: by 10.223.53.201 with SMTP id n9mr2452195fag.26.1315309886930; Tue, 06 Sep 2011 04:51:26 -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.26 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 06 Sep 2011 04:51:26 -0700 (PDT) From: David Herrmann To: linux-input@vger.kernel.org Cc: jkosina@suse.cz, David Herrmann Subject: [PATCH v2 14/15] HID: wiimote: Read wiimote battery charge level Date: Tue, 6 Sep 2011 13:50:39 +0200 Message-Id: <1315309840-4664-15-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:29 +0000 (UTC) This registers a power_supply device for every remote to retrieve the current battery charge level. Since this information is not sent by the wiimote continously, we need to explicitely request it. Signed-off-by: David Herrmann --- drivers/hid/Kconfig | 1 + drivers/hid/hid-wiimote.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 0 deletions(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 1130a89..9907dc9 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -590,6 +590,7 @@ config HID_WIIMOTE tristate "Nintendo Wii Remote support" depends on BT_HIDP depends on LEDS_CLASS + select POWER_SUPPLY ---help--- Support for the Nintendo Wii Remote bluetooth device. diff --git a/drivers/hid/hid-wiimote.c b/drivers/hid/hid-wiimote.c index 48198cb..c83cafa 100644 --- a/drivers/hid/hid-wiimote.c +++ b/drivers/hid/hid-wiimote.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "hid-ids.h" @@ -51,6 +52,7 @@ struct wiimote_data { struct led_classdev *leds[4]; struct input_dev *accel; struct input_dev *ir; + struct power_supply battery; spinlock_t qlock; __u8 head; @@ -133,6 +135,10 @@ static __u16 wiiproto_keymap[] = { BTN_MODE, /* WIIPROTO_KEY_HOME */ }; +static enum power_supply_property wiimote_battery_props[] = { + POWER_SUPPLY_PROP_CAPACITY +}; + /* requires the state.lock spinlock to be held */ static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, __u32 opt) @@ -453,6 +459,43 @@ static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, return ret; } +static int wiimote_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct wiimote_data *wdata = container_of(psy, + struct wiimote_data, battery); + int ret = 0, state; + unsigned long flags; + + ret = wiimote_cmd_acquire(wdata); + if (ret) + return ret; + + spin_lock_irqsave(&wdata->state.lock, flags); + wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); + wiiproto_req_status(wdata); + spin_unlock_irqrestore(&wdata->state.lock, flags); + + ret = wiimote_cmd_wait(wdata); + state = wdata->state.cmd_battery; + wiimote_cmd_release(wdata); + + if (ret) + return ret; + + switch (psp) { + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = state * 100 / 255; + break; + default: + ret = -EINVAL; + break; + } + + return ret; +} + static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) { int ret; @@ -1155,6 +1198,7 @@ static void wiimote_destroy(struct wiimote_data *wdata) { wiimote_leds_destroy(wdata); + power_supply_unregister(&wdata->battery); input_unregister_device(wdata->accel); input_unregister_device(wdata->ir); input_unregister_device(wdata->input); @@ -1206,6 +1250,19 @@ static int wiimote_hid_probe(struct hid_device *hdev, goto err_input; } + wdata->battery.properties = wiimote_battery_props; + wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props); + wdata->battery.get_property = wiimote_battery_get_property; + wdata->battery.name = "wiimote_battery"; + wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY; + wdata->battery.use_for_apm = 0; + + ret = power_supply_register(&wdata->hdev->dev, &wdata->battery); + if (ret) { + hid_err(hdev, "Cannot register battery device\n"); + goto err_battery; + } + ret = wiimote_leds_create(wdata); if (ret) goto err_free; @@ -1223,6 +1280,9 @@ err_free: wiimote_destroy(wdata); return ret; +err_battery: + input_unregister_device(wdata->input); + wdata->input = NULL; err_input: input_unregister_device(wdata->ir); wdata->ir = NULL;