public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: linux-input@vger.kernel.org, Tim Schumacher <timschumi@gmx.de>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 11/20] Input: iforce - factor out hat handling when parsing packets
Date: Mon, 17 Sep 2018 17:47:23 -0700	[thread overview]
Message-ID: <20180918004732.9875-11-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <20180918004732.9875-1-dmitry.torokhov@gmail.com>

This makes code clearer a bit.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 .../input/joystick/iforce/iforce-packets.c    | 101 ++++++++++--------
 1 file changed, 57 insertions(+), 44 deletions(-)

diff --git a/drivers/input/joystick/iforce/iforce-packets.c b/drivers/input/joystick/iforce/iforce-packets.c
index 7aba2966454a..ffd1dd65deb8 100644
--- a/drivers/input/joystick/iforce/iforce-packets.c
+++ b/drivers/input/joystick/iforce/iforce-packets.c
@@ -130,11 +130,47 @@ static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
 	return -1;
 }
 
-void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
+static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
 {
 	struct input_dev *dev = iforce->dev;
 	int i;
 
+	input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
+	input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
+
+	for (i = 0; iforce->type->btn[i] >= 0; i++)
+		input_report_key(dev, iforce->type->btn[i],
+				 data[(i >> 3) + 5] & (1 << (i & 7)));
+
+	/* If there are untouched bits left, interpret them as the second hat */
+	if (i <= 8) {
+		u8 btns = data[6];
+
+		if (test_bit(ABS_HAT1X, dev->absbit)) {
+			if (btns & BIT(3))
+				input_report_abs(dev, ABS_HAT1X, -1);
+			else if (btns & BIT(1))
+				input_report_abs(dev, ABS_HAT1X, 1);
+			else
+				input_report_abs(dev, ABS_HAT1X, 0);
+		}
+
+		if (test_bit(ABS_HAT1Y, dev->absbit)) {
+			if (btns & BIT(0))
+				input_report_abs(dev, ABS_HAT1Y, -1);
+			else if (btns & BIT(2))
+				input_report_abs(dev, ABS_HAT1Y, 1);
+			else
+				input_report_abs(dev, ABS_HAT1Y, 0);
+		}
+	}
+}
+
+void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
+{
+	struct input_dev *dev = iforce->dev;
+	int i, j;
+
 	wake_up(&iforce->wait);
 
 	if (!iforce->type)
@@ -143,48 +179,26 @@ void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
 	switch (HI(cmd)) {
 
 	case 0x01:	/* joystick position data */
-	case 0x03:	/* wheel position data */
-		if (HI(cmd) == 1) {
-			input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
-			input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
-			input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
-			if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
-				input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
-		} else {
-			input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
-			input_report_abs(dev, ABS_GAS,   255 - data[2]);
-			input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
-		}
+		input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
+		input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
+		input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
 
-		input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
-		input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
-
-		for (i = 0; iforce->type->btn[i] >= 0; i++)
-			input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
-
-		/* If there are untouched bits left, interpret them as the second hat */
-		if (i <= 8) {
-			int btns = data[6];
-			if (test_bit(ABS_HAT1X, dev->absbit)) {
-				if (btns & 8)
-					input_report_abs(dev, ABS_HAT1X, -1);
-				else if (btns & 2)
-					input_report_abs(dev, ABS_HAT1X, 1);
-				else
-					input_report_abs(dev, ABS_HAT1X, 0);
-			}
-			if (test_bit(ABS_HAT1Y, dev->absbit)) {
-				if (btns & 1)
-					input_report_abs(dev, ABS_HAT1Y, -1);
-				else if (btns & 4)
-					input_report_abs(dev, ABS_HAT1Y, 1);
-				else
-					input_report_abs(dev, ABS_HAT1Y, 0);
-			}
-		}
+		if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
+			input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
+
+		iforce_report_hats_buttons(iforce, data);
 
 		input_sync(dev);
+		break;
 
+	case 0x03:	/* wheel position data */
+		input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
+		input_report_abs(dev, ABS_GAS,   255 - data[2]);
+		input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
+
+		iforce_report_hats_buttons(iforce, data);
+
+		input_sync(dev);
 		break;
 
 	case 0x02:	/* status report */
@@ -202,11 +216,10 @@ void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
 			/* Report stop event */
 			input_report_ff_status(dev, i, FF_STATUS_STOPPED);
 		}
-		if (LO(cmd) > 3) {
-			int j;
-			for (j = 3; j < LO(cmd); j += 2)
-				mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
-		}
+
+		for (j = 3; j < LO(cmd); j += 2)
+			mark_core_as_ready(iforce, data[j] | (data[j + 1] << 8));
+
 		break;
 	}
 }
-- 
2.19.0.397.gdd90340f6a-goog


  parent reply	other threads:[~2018-09-18  0:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-18  0:47 [PATCH 01/20] Input: iforce - remove "being used" silliness Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 02/20] Input: iforce - introduce transport ops Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 03/20] Input: iforce - move get_id to the transport operations Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 04/20] Input: iforce - move command completion handling to serio code Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 05/20] Input: iforce - introduce start and stop io transport ops Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 06/20] Input: iforce - add bus type and parent arguments to iforce_init_device() Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 07/20] Input: iforce - move transport data into transport modules Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 08/20] Input: iforce - split into core and " Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 09/20] Input: iforce - use DMA-safe buffer when getting IDs from USB Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 10/20] Input: iforce - update formatting of switch statements Dmitry Torokhov
2018-09-18  0:47 ` Dmitry Torokhov [this message]
2018-09-18  0:47 ` [PATCH 12/20] Input: iforce - do not combine arguments for iforce_process_packet() Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 13/20] Input: iforce - signal command completion from transport code Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 14/20] Input: iforce - only call iforce_process_packet() if initialized Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 15/20] Input: iforce - allow callers supply data buffer when fetching device IDs Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 16/20] Input: iforce - use DMA-safe buffores for USB transfers Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 17/20] Input: only credit entropy when events are generated by a device Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 18/20] Input: iforce - drop bus type from iforce structure Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 19/20] Input: iforce - drop couple of temps from transport code Dmitry Torokhov
2018-09-18  0:47 ` [PATCH 20/20] Input: iforce - use unaligned accessors, where appropriate Dmitry Torokhov
2018-09-19 14:51 ` [PATCH 01/20] Input: iforce - remove "being used" silliness Tim Schumacher
2018-09-19 17:10   ` Dmitry Torokhov
2019-06-12 14:44     ` Tim Schumacher
2019-06-19  0:24       ` Dmitry Torokhov

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=20180918004732.9875-11-dmitry.torokhov@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=timschumi@gmx.de \
    /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