From patchwork Mon Aug 29 13:38:11 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 1108072 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 p7TDclf6003316 for ; Mon, 29 Aug 2011 13:38:52 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753775Ab1H2Niw (ORCPT ); Mon, 29 Aug 2011 09:38:52 -0400 Received: from mail-fx0-f46.google.com ([209.85.161.46]:57851 "EHLO mail-fx0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753769Ab1H2Niv (ORCPT ); Mon, 29 Aug 2011 09:38:51 -0400 Received: by mail-fx0-f46.google.com with SMTP id 19so4422164fxh.19 for ; Mon, 29 Aug 2011 06:38:51 -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=qPc1WWOxz32L2RP7AXyiZQw+DHA1Y4TqkbGthWgnUqc=; b=P9LCwLN9grgQoCa5tf40qXL5FstACCITaUuQ8AYmklDJ3pI2e0IZt3FQXZ4oEpziGQ dix9Nxbq2SYiKs2TZ0GGePUsmh4Ciwz1sSElQ60zrVA0xBNOEPf720WgqoxrOCqVsxFF yKAq8GLjZ7uHGXs4IvQLeBQz+1/qOD0+dzO/o= Received: by 10.223.10.143 with SMTP id p15mr7065022fap.12.1314625131287; Mon, 29 Aug 2011 06:38:51 -0700 (PDT) Received: from localhost.localdomain (stgt-5f70ad61.pool.mediaWays.net [95.112.173.97]) by mx.google.com with ESMTPS id b14sm3767290fak.29.2011.08.29.06.38.49 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 29 Aug 2011 06:38:50 -0700 (PDT) From: David Herrmann To: linux-input@vger.kernel.org Cc: jkosina@suse.cz, padovan@profusion.mobi, oliver@neukum.org, David Herrmann Subject: [PATCH 14/14] HID: wiimote: Read wiimote battery charge level Date: Mon, 29 Aug 2011 15:38:11 +0200 Message-Id: <1314625091-1405-15-git-send-email-dh.herrmann@googlemail.com> X-Mailer: git-send-email 1.7.6.1 In-Reply-To: <1314625091-1405-1-git-send-email-dh.herrmann@googlemail.com> References: <1314625091-1405-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]); Mon, 29 Aug 2011 13:38:52 +0000 (UTC) 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 --- 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 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//battery +Date: July 2011 +KernelVersion: 3.2 +Contact: David Herrmann +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 #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;