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 03/16] HID: wiimote: Add read-mem helpers
Date: Sun, 13 Nov 2011 14:27:06 +0100	[thread overview]
Message-ID: <1321190839-3517-4-git-send-email-dh.herrmann@googlemail.com> (raw)
In-Reply-To: <1321190839-3517-1-git-send-email-dh.herrmann@googlemail.com>

Add helper functions similar to the write-mem helpers but for reading wiimote
memory and eeprom.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
---
 drivers/hid/hid-wiimote-core.c |   77 ++++++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-wiimote.h      |    4 ++
 2 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c
index c955d50..f7f8b7f 100644
--- a/drivers/hid/hid-wiimote-core.c
+++ b/drivers/hid/hid-wiimote-core.c
@@ -312,6 +312,37 @@ static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
 	wiimote_queue(wdata, cmd, sizeof(cmd));
 }
 
+#define wiiproto_req_rreg(wdata, os, sz) \
+				wiiproto_req_rmem((wdata), false, (os), (sz))
+
+#define wiiproto_req_reeprom(wdata, os, sz) \
+				wiiproto_req_rmem((wdata), true, (os), (sz))
+
+static void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
+						__u32 offset, __u16 size)
+{
+	__u8 cmd[7];
+
+	if (size == 0) {
+		hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
+		return;
+	}
+
+	cmd[0] = WIIPROTO_REQ_RMEM;
+	cmd[1] = 0;
+	cmd[2] = (offset >> 16) & 0xff;
+	cmd[3] = (offset >> 8) & 0xff;
+	cmd[4] = offset & 0xff;
+	cmd[5] = (size >> 8) & 0xff;
+	cmd[6] = size & 0xff;
+
+	if (!eeprom)
+		cmd[1] |= 0x04;
+
+	wiiproto_keep_rumble(wdata, &cmd[1]);
+	wiimote_queue(wdata, cmd, sizeof(cmd));
+}
+
 /* requries the cmd-mutex to be held */
 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
 						const __u8 *wmem, __u8 size)
@@ -331,6 +362,36 @@ int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
 	return ret;
 }
 
+/* requries the cmd-mutex to be held */
+ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
+								__u8 size)
+{
+	unsigned long flags;
+	ssize_t ret;
+
+	spin_lock_irqsave(&wdata->state.lock, flags);
+	wdata->state.cmd_read_size = size;
+	wdata->state.cmd_read_buf = rmem;
+	wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
+	wiiproto_req_rreg(wdata, offset, size);
+	spin_unlock_irqrestore(&wdata->state.lock, flags);
+
+	ret = wiimote_cmd_wait(wdata);
+
+	spin_lock_irqsave(&wdata->state.lock, flags);
+	wdata->state.cmd_read_buf = NULL;
+	spin_unlock_irqrestore(&wdata->state.lock, flags);
+
+	if (!ret) {
+		if (wdata->state.cmd_read_size == 0)
+			ret = -EIO;
+		else
+			ret = wdata->state.cmd_read_size;
+	}
+
+	return ret;
+}
+
 static int wiimote_battery_get_property(struct power_supply *psy,
 						enum power_supply_property psp,
 						union power_supply_propval *val)
@@ -742,7 +803,23 @@ static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
 
 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
 {
+	__u16 offset = payload[3] << 8 | payload[4];
+	__u8 size = (payload[2] >> 4) + 1;
+	__u8 err = payload[2] & 0x0f;
+
 	handler_keys(wdata, payload);
+
+	if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
+		if (err)
+			size = 0;
+		else if (size > wdata->state.cmd_read_size)
+			size = wdata->state.cmd_read_size;
+
+		wdata->state.cmd_read_size = size;
+		if (wdata->state.cmd_read_buf)
+			memcpy(wdata->state.cmd_read_buf, &payload[5], size);
+		wiimote_cmd_complete(wdata);
+	}
 }
 
 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h
index d78f79c..865740d 100644
--- a/drivers/hid/hid-wiimote.h
+++ b/drivers/hid/hid-wiimote.h
@@ -62,6 +62,8 @@ struct wiimote_state {
 	/* results of synchronous requests */
 	__u8 cmd_battery;
 	__u8 cmd_err;
+	__u8 *cmd_read_buf;
+	__u8 cmd_read_size;
 };
 
 struct wiimote_data {
@@ -113,6 +115,8 @@ enum wiiproto_reqs {
 extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
 extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
 						const __u8 *wmem, __u8 size);
+extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
+							__u8 *rmem, __u8 size);
 
 /* requires the state.lock spinlock to be held */
 static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
-- 
1.7.7.3


  parent reply	other threads:[~2011-11-13 13:27 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-13 13:27 [PATCH 00/16] Wiimote: Extension and Debug Support David Herrmann
2011-11-13 13:27 ` [PATCH 01/16] HID: wiimote: Rename driver to allow multiple source files David Herrmann
2011-11-13 13:27 ` [PATCH 02/16] HID: wiimote: Move common symbols into header David Herrmann
2011-11-13 13:27 ` David Herrmann [this message]
2011-11-13 13:27 ` [PATCH 04/16] HID: wiimote: Add extension support stub David Herrmann
2011-11-14  7:54   ` Oliver Neukum
2011-11-14 11:26     ` David Herrmann
2011-11-14 11:35       ` Oliver Neukum
2011-11-15 19:24         ` David Herrmann
2011-11-13 13:27 ` [PATCH 05/16] HID: wiimote: Add extension initializer stubs David Herrmann
2011-11-13 13:27 ` [PATCH 06/16] HID: wiimote: Add extension initializers David Herrmann
2011-11-13 13:27 ` [PATCH 07/16] HID: wiimote: Add extension sysfs attribute David Herrmann
2011-11-13 13:27 ` [PATCH 08/16] HID: wiimote: Register input devices for extensions David Herrmann
2011-11-13 13:27 ` [PATCH 09/16] HID: wiimote: Add extension handler stubs David Herrmann
2011-11-13 13:27 ` [PATCH 10/16] HID: wiimote: Parse motion+ data David Herrmann
2011-11-13 13:27 ` [PATCH 11/16] HID: wiimote: Parse nunchuck data David Herrmann
2011-11-13 13:27 ` [PATCH 12/16] HID: wiimote: Parse classic controller data David Herrmann
2011-11-13 13:27 ` [PATCH 13/16] HID: wiimote: Add debugfs support stubs David Herrmann
2011-11-13 13:27 ` [PATCH 14/16] HID: wiimote: Allow direct eeprom access David Herrmann
2011-11-13 13:27 ` [PATCH 15/16] HID: wiimote: Allow direct DRM debug access David Herrmann
2011-11-13 13:27 ` [PATCH 16/16] HID: wiimote: Enable NO_INIT_REPORTS quirk David Herrmann
2011-11-16 17:05   ` Jiri Kosina
2011-11-16 17:49     ` David Herrmann
2011-11-16 18:39       ` Jiri Kosina
2011-11-17 13:11         ` 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=1321190839-3517-4-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).