[PATCH 14/14] HID: wiimote: Read wiimote battery charge level
flat view
STALE5518d
From: David Herrmann <hidden>
Date: 2011-08-29 13:38:51
Subsystem:
hid core layer, the rest, wiimote hid driver · Maintainers:
Jiri Kosina, Benjamin Tissoires, Linus Torvalds, David Rheinsberg
Revision v1 of 3 in this series.
Revisions (3)
- v1 [diff vs current]
- v1 current
- v2 [diff vs current]
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 <redacted> --- 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