linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@googlemail.com>
To: linux-input@vger.kernel.org
Cc: jkosina@suse.cz, David Herrmann <dh.herrmann@googlemail.com>
Subject: [PATCH v2 14/15] HID: wiimote: Read wiimote battery charge level
Date: Tue,  6 Sep 2011 13:50:39 +0200	[thread overview]
Message-ID: <1315309840-4664-15-git-send-email-dh.herrmann@googlemail.com> (raw)
In-Reply-To: <1315309840-4664-1-git-send-email-dh.herrmann@googlemail.com>

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 <dh.herrmann@googlemail.com>
---
 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 <linux/leds.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/power_supply.h>
 #include <linux/spinlock.h>
 #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;
-- 
1.7.6.1


  parent reply	other threads:[~2011-09-06 11:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-06 11:50 [PATCH v2 00/15] Extended Wiimote Support David Herrmann
2011-09-06 11:50 ` [PATCH v2 01/15] HID: wiimote: Support rumble device David Herrmann
2011-09-06 11:50 ` [PATCH v2 02/15] HID: wiimote: Add force-feedback support David Herrmann
2011-09-06 11:50 ` [PATCH v2 03/15] HID: wiimote: Add accelerometer input device David Herrmann
2011-09-06 11:50 ` [PATCH v2 04/15] HID: wiimote: Parse accelerometer data David Herrmann
2011-09-06 11:50 ` [PATCH v2 05/15] HID: wiimote: Add IR input device David Herrmann
2011-09-06 11:50 ` [PATCH v2 06/15] HID: wiimote: Parse IR data David Herrmann
2011-09-06 11:50 ` [PATCH v2 07/15] HID: wiimote: Add missing extension DRM handlers David Herrmann
2011-09-06 11:50 ` [PATCH v2 08/15] HID: wiimote: Add register/eeprom memory support David Herrmann
2011-09-06 11:50 ` [PATCH v2 09/15] HID: wiimote: Helper functions for synchronous requests David Herrmann
2011-09-06 11:50 ` [PATCH v2 10/15] HID: wiimote: Add write-register helpers David Herrmann
2011-09-06 11:50 ` [PATCH v2 11/15] HID: wiimote: Add IR initializer David Herrmann
2011-09-06 11:50 ` [PATCH v2 12/15] HID: wiimote: Initialize IR cam on request David Herrmann
2011-09-06 11:50 ` [PATCH v2 13/15] HID: wiimote: Add status request David Herrmann
2011-09-06 11:50 ` David Herrmann [this message]
2011-09-06 11:50 ` [PATCH v2 15/15] HID: wiimote: Add MAINTAINERS entry David Herrmann
2011-09-07 11:29 ` [PATCH v2 00/15] Extended Wiimote Support Jiri Kosina

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1315309840-4664-15-git-send-email-dh.herrmann@googlemail.com \
    --to=dh.herrmann@googlemail.com \
    --cc=jkosina@suse.cz \
    --cc=linux-input@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).