From ff71a94e11bfbf0f45ddbeb7522de815515b32b9 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megous@megous.com>
Date: Sat, 28 Sep 2019 16:01:06 +0200
Subject: [PATCH 057/446] input: cyttsp4: De-obfuscate MT signals
 setup/platform data

Original code does a lot of array math, and is needlessly complicated.
Make it easier to use and clean it up.

Signed-off-by: Ondrej Jirman <megous@megous.com>
---
 drivers/input/touchscreen/cyttsp4_core.c | 102 ++++++++++-------------
 drivers/input/touchscreen/cyttsp4_core.h |  26 ------
 include/linux/platform_data/cyttsp4.h    |  27 ++++--
 3 files changed, 66 insertions(+), 89 deletions(-)

diff --git a/drivers/input/touchscreen/cyttsp4_core.c b/drivers/input/touchscreen/cyttsp4_core.c
index c47a01362..be8c460d4 100644
--- a/drivers/input/touchscreen/cyttsp4_core.c
+++ b/drivers/input/touchscreen/cyttsp4_core.c
@@ -853,31 +853,26 @@ static void cyttsp4_get_mt_touches(struct cyttsp4 *cd, int num_cur_tch)
 	struct device *dev = &cd->input->dev;
 	struct cyttsp4_sysinfo *si = cd->si;
 	struct cyttsp4_touch tch;
-	int sig;
 	int i, j, t = 0;
 	int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
+	struct cyttsp4_signal_def* sig;
 
 	memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
 	for (i = 0; i < num_cur_tch; i++) {
 		cyttsp4_get_touch(cd, &tch, si->xy_data +
-			(i * si->si_ofs.tch_rec_size));
-		if ((tch.abs[CY_TCH_T] < cd->pdata->frmwrk->abs
-			[(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
-			(tch.abs[CY_TCH_T] > cd->pdata->frmwrk->abs
-			[(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
+				  (i * si->si_ofs.tch_rec_size));
+
+		sig = &cd->pdata->signals[CY_ABS_ID_OST];
+		if (tch.abs[CY_TCH_T] < sig->min ||
+			tch.abs[CY_TCH_T] > sig->max) {
 			dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
-				__func__, i, tch.abs[CY_TCH_T],
-				cd->pdata->frmwrk->abs[(CY_ABS_ID_OST *
-				CY_NUM_ABS_SET) + CY_MAX_OST]);
+				__func__, i, tch.abs[CY_TCH_T], sig->max);
 			continue;
 		}
 
 		/* use 0 based track id's */
-		sig = cd->pdata->frmwrk->abs
-			[(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
-		if (sig != CY_IGNORE_VALUE) {
-			t = tch.abs[CY_TCH_T] - cd->pdata->frmwrk->abs
-				[(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
+		if (sig->signal >= 0) {
+			t = tch.abs[CY_TCH_T] - sig->min;
 			if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
 				dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
 					__func__, t, tch.abs[CY_TCH_E]);
@@ -885,18 +880,18 @@ static void cyttsp4_get_mt_touches(struct cyttsp4 *cd, int num_cur_tch)
 			}
 			input_mt_slot(cd->input, t);
 			input_mt_report_slot_state(cd->input, MT_TOOL_FINGER,
-					true);
+						   true);
 			ids[t] = true;
 		}
 
 		/* all devices: position and pressure fields */
 		for (j = 0; j <= CY_ABS_W_OST; j++) {
-			sig = cd->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
-				CY_NUM_ABS_SET) + 0];
-			if (sig != CY_IGNORE_VALUE)
-				input_report_abs(cd->input, sig,
-					tch.abs[CY_TCH_X + j]);
+			sig = &cd->pdata->signals[j];
+			if (sig->signal >= 0)
+				input_report_abs(cd->input, sig->signal,
+						 tch.abs[CY_TCH_X + j]);
 		}
+
 		if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
 			/*
 			 * TMA400 size and orientation fields:
@@ -909,12 +904,10 @@ static void cyttsp4_get_mt_touches(struct cyttsp4 *cd, int num_cur_tch)
 
 			/* Get the extended touch fields */
 			for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
-				sig = cd->pdata->frmwrk->abs
-					[((CY_ABS_MAJ_OST + j) *
-					CY_NUM_ABS_SET) + 0];
-				if (sig != CY_IGNORE_VALUE)
-					input_report_abs(cd->input, sig,
-						tch.abs[CY_TCH_MAJ + j]);
+				sig = &cd->pdata->signals[CY_ABS_MAJ_OST + j];
+				if (sig->signal >= 0)
+					input_report_abs(cd->input, sig->signal,
+							 tch.abs[CY_TCH_MAJ + j]);
 			}
 		}
 
@@ -1884,7 +1877,7 @@ static void cyttsp4_mt_close(struct input_dev *input)
 static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
 {
 	struct device *dev = cd->dev;
-	int signal = CY_IGNORE_VALUE;
+	struct cyttsp4_signal_def *signal;
 	int max_x, max_y, max_p, min, max;
 	int max_x_tmp, max_y_tmp;
 	int i;
@@ -1909,37 +1902,30 @@ static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
 	max_p = cd->si->si_ofs.max_p;
 
 	/* set event signal capabilities */
-	for (i = 0; i < (cd->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
-		signal = cd->pdata->frmwrk->abs
-			[(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
-		if (signal != CY_IGNORE_VALUE) {
-			__set_bit(signal, cd->input->absbit);
-			min = cd->pdata->frmwrk->abs
-				[(i * CY_NUM_ABS_SET) + CY_MIN_OST];
-			max = cd->pdata->frmwrk->abs
-				[(i * CY_NUM_ABS_SET) + CY_MAX_OST];
-			if (i == CY_ABS_ID_OST) {
-				/* shift track ids down to start at 0 */
-				max = max - min;
-				min = min - min;
-			} else if (i == CY_ABS_X_OST)
-				max = max_x;
-			else if (i == CY_ABS_Y_OST)
-				max = max_y;
-			else if (i == CY_ABS_P_OST)
-				max = max_p;
-			input_set_abs_params(cd->input, signal, min, max,
-				cd->pdata->frmwrk->abs
-				[(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
-				cd->pdata->frmwrk->abs
-				[(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
-			dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
-				__func__, signal, min, max);
-			if ((i == CY_ABS_ID_OST) &&
-				(cd->si->si_ofs.tch_rec_size <
-				CY_TMA4XX_TCH_REC_SIZE))
-				break;
-		}
+	for (i = 0; i < cd->pdata->n_signals; i++) {
+		signal = &cd->pdata->signals[i];
+
+		__set_bit(signal->signal, cd->input->absbit);
+
+		max = signal->max;
+		min = signal->min;
+
+		if (signal->signal == ABS_MT_POSITION_X)
+			max = max_x;
+		else if (signal->signal == ABS_MT_POSITION_Y)
+			max = max_y;
+		else if (signal->signal == ABS_MT_PRESSURE)
+			max = max_p;
+
+		input_set_abs_params(cd->input, signal->signal, min, max,
+				     signal->fuzz, signal->flat);
+		dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
+			__func__, signal->signal, min, max);
+
+		if ((i == CY_ABS_ID_OST) &&
+			(cd->si->si_ofs.tch_rec_size <
+			CY_TMA4XX_TCH_REC_SIZE))
+			break;
 	}
 
 	input_mt_init_slots(cd->input, cd->si->si_ofs.tch_abs[CY_TCH_T].max,
diff --git a/drivers/input/touchscreen/cyttsp4_core.h b/drivers/input/touchscreen/cyttsp4_core.h
index 05fb30058..995bfd0a5 100644
--- a/drivers/input/touchscreen/cyttsp4_core.h
+++ b/drivers/input/touchscreen/cyttsp4_core.h
@@ -357,32 +357,6 @@ enum cyttsp4_hst_mode_bits {
 	CY_HST_RESET       = (1 << 0),
 };
 
-/* abs settings */
-#define CY_IGNORE_VALUE			0xFFFF
-
-/* abs signal capabilities offsets in the frameworks array */
-enum cyttsp4_sig_caps {
-	CY_SIGNAL_OST,
-	CY_MIN_OST,
-	CY_MAX_OST,
-	CY_FUZZ_OST,
-	CY_FLAT_OST,
-	CY_NUM_ABS_SET	/* number of signal capability fields */
-};
-
-/* abs axis signal offsets in the framworks array  */
-enum cyttsp4_sig_ost {
-	CY_ABS_X_OST,
-	CY_ABS_Y_OST,
-	CY_ABS_P_OST,
-	CY_ABS_W_OST,
-	CY_ABS_ID_OST,
-	CY_ABS_MAJ_OST,
-	CY_ABS_MIN_OST,
-	CY_ABS_OR_OST,
-	CY_NUM_ABS_OST	/* number of abs signals */
-};
-
 enum cyttsp4_flags {
 	CY_FLAG_NONE = 0x00,
 	CY_FLAG_HOVER = 0x04,
diff --git a/include/linux/platform_data/cyttsp4.h b/include/linux/platform_data/cyttsp4.h
index 362fa181a..1b5b30796 100644
--- a/include/linux/platform_data/cyttsp4.h
+++ b/include/linux/platform_data/cyttsp4.h
@@ -21,15 +21,31 @@
 
 #define CY_TOUCH_SETTINGS_MAX 32
 
-struct touch_framework {
-	const uint16_t  *abs;
-	uint8_t         size;
-} __packed;
+/* abs axis signal offsets in the signals array  */
+enum cyttsp4_sig_ost {
+	CY_ABS_X_OST,
+	CY_ABS_Y_OST,
+	CY_ABS_P_OST,
+	CY_ABS_W_OST,
+	CY_ABS_ID_OST,
+	CY_ABS_MAJ_OST,
+	CY_ABS_MIN_OST,
+	CY_ABS_OR_OST,
+	CY_NUM_ABS_OST	/* number of abs signals */
+};
 
 struct cyttsp4_virtual_key {
 	int code;
 };
 
+struct cyttsp4_signal_def {
+	int signal;
+	int min;
+	int max;
+	int fuzz;
+	int flat;
+};
+
 struct cyttsp4_platform_data {
 	char const *inp_dev_name;
 	unsigned short flags;
@@ -46,7 +62,8 @@ struct cyttsp4_platform_data {
 	int (*irq_stat)(struct cyttsp4_platform_data *pdata,
 		struct device *dev);
 
-	struct touch_framework *frmwrk;
+	int n_signals;
+	struct cyttsp4_signal_def *signals;
 
 	int n_keys;
 	struct cyttsp4_virtual_key* keys;
-- 
2.31.1

