All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@googlemail.com>
To: linux-input@vger.kernel.org
Cc: jkosina@suse.cz, padovan@profusion.mobi, oliver@neukum.org,
	David Herrmann <dh.herrmann@googlemail.com>
Subject: [PATCH 14/14] HID: wiimote: Read wiimote battery charge level
Date: Mon, 29 Aug 2011 15:38:11 +0200	[thread overview]
Message-ID: <1314625091-1405-15-git-send-email-dh.herrmann@googlemail.com> (raw)
In-Reply-To: <1314625091-1405-1-git-send-email-dh.herrmann@googlemail.com>

This adds a new sysfs file for wiimotes which returns the current battery charge
level of the device. Since this information is not sent by the wiimote
continously, we need to explicitely request it.

Also bump version number since the core driver is feature complete now.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
---
 Documentation/ABI/testing/sysfs-driver-hid-wiimote |    8 ++++
 drivers/hid/hid-wiimote.c                          |   38 +++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-hid-wiimote b/Documentation/ABI/testing/sysfs-driver-hid-wiimote
index 5d5a16e..817aa82 100644
--- a/Documentation/ABI/testing/sysfs-driver-hid-wiimote
+++ b/Documentation/ABI/testing/sysfs-driver-hid-wiimote
@@ -8,3 +8,11 @@ Contact:	David Herrmann <dh.herrmann@googlemail.com>
 Description:	Make it possible to set/get current led state. Reading from it
 		returns 0 if led is off and 1 if it is on. Writing 0 to it
 		disables the led, writing 1 enables it.
+
+What:		/sys/bus/hid/drivers/wiimote/<dev>/battery
+Date:		July 2011
+KernelVersion:	3.2
+Contact:	David Herrmann <dh.herrmann@googlemail.com>
+Description:	Readonly attribute which returns the current battery charge
+		level. An integer between 0 and 255 is returned, 0 means empty
+		and 255 full.
diff --git a/drivers/hid/hid-wiimote.c b/drivers/hid/hid-wiimote.c
index 48198cb..271630c 100644
--- a/drivers/hid/hid-wiimote.c
+++ b/drivers/hid/hid-wiimote.c
@@ -20,7 +20,7 @@
 #include <linux/spinlock.h>
 #include "hid-ids.h"
 
-#define WIIMOTE_VERSION "0.1"
+#define WIIMOTE_VERSION "0.2"
 #define WIIMOTE_NAME "Nintendo Wii Remote"
 #define WIIMOTE_BUFSIZE 32
 
@@ -133,6 +133,9 @@ static __u16 wiiproto_keymap[] = {
 	BTN_MODE,	/* WIIPROTO_KEY_HOME */
 };
 
+#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
+									dev))
+
 /* requires the state.lock spinlock to be held */
 static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
 								__u32 opt)
@@ -638,6 +641,34 @@ static int wiimote_ff_play(struct input_dev *dev, void *data,
 	return 0;
 }
 
+static ssize_t wiifs_battery_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	struct wiimote_data *wdata = dev_to_wii(dev);
+	unsigned long flags;
+	int state, ret;
+
+	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;
+
+	return sprintf(buf, "%d\n", state);
+}
+
+static DEVICE_ATTR(battery, S_IRUGO, wiifs_battery_show, NULL);
+
 static int wiimote_input_open(struct input_dev *dev)
 {
 	struct wiimote_data *wdata = input_get_drvdata(dev);
@@ -1154,6 +1185,7 @@ err:
 static void wiimote_destroy(struct wiimote_data *wdata)
 {
 	wiimote_leds_destroy(wdata);
+	device_remove_file(&wdata->hdev->dev, &dev_attr_battery);
 
 	input_unregister_device(wdata->accel);
 	input_unregister_device(wdata->ir);
@@ -1206,6 +1238,10 @@ static int wiimote_hid_probe(struct hid_device *hdev,
 		goto err_input;
 	}
 
+	ret = device_create_file(&hdev->dev, &dev_attr_battery);
+	if (ret)
+		goto err_free;
+
 	ret = wiimote_leds_create(wdata);
 	if (ret)
 		goto err_free;
-- 
1.7.6.1


  parent reply	other threads:[~2011-08-29 13:38 UTC|newest]

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

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=1314625091-1405-15-git-send-email-dh.herrmann@googlemail.com \
    --to=dh.herrmann@googlemail.com \
    --cc=jkosina@suse.cz \
    --cc=linux-input@vger.kernel.org \
    --cc=oliver@neukum.org \
    --cc=padovan@profusion.mobi \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.