Revert "extra: Update touchpad updater for newer ELAN firmwares"

This reverts commit cdde5f153d4b5e675794bc07cf8fc410a0c3fcf1.

Reason for revert: Breaks emerge ec-utils

Original change's description:
> extra: Update touchpad updater for newer ELAN firmwares
>
> Update the update logic in the touch updater for newer ELAN firmwares
> requires to send different commands for getting the correct ic type and
> iap version.
>
> BUG=b:304206879
> TEST=emerge ec-devutils and copy ec_touchpad_updater on device, then run
>      `ec_touchpad_updater -v $VID -p $PID -f ${TOUCH_FIRMWARE}`, and can
>      update the touchpad.
>
> Change-Id: If443abe73918940340d515232981a4ef02e8a7f8
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4930456
> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
> Tested-by: Sung-Chi Li <lschyi@chromium.org>
> Commit-Queue: Sung-Chi Li <lschyi@chromium.org>
> Reviewed-by: Ting Shen <phoenixshen@chromium.org>

BUG=b:304206879

Change-Id: I931caebb873e5f8cf975ee1703991eb9f084b9ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4948271
Auto-Submit: Jeremy Bettis <jbettis@chromium.org>
Commit-Queue: Jeremy Bettis <jbettis@chromium.org>
Tested-by: Jeremy Bettis <jbettis@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Reviewed-by: Sung-Chi Li <lschyi@chromium.org>
diff --git a/extra/touchpad_updater/touchpad_updater.c b/extra/touchpad_updater/touchpad_updater.c
index 94f2ed8..608a847 100644
--- a/extra/touchpad_updater/touchpad_updater.c
+++ b/extra/touchpad_updater/touchpad_updater.c
@@ -11,7 +11,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <endian.h>
 #include <getopt.h>
 #include <libusb.h>
 #include <poll.h>
@@ -29,13 +28,12 @@
 #define MAX_FW_PAGE_SIZE 512
 #define MAX_FW_PAGE_COUNT 1024
 #define MAX_FW_SIZE (128 * 1024)
-#define I2C_RESPONSE_OFFSET 4
 
 static uint8_t fw_data[MAX_FW_SIZE];
 int fw_page_count;
 int fw_page_size;
 int fw_size;
-uint16_t ic_type;
+uint8_t ic_type;
 int iap_version;
 
 /* Utility functions */
@@ -380,17 +378,26 @@
 }
 
 /* Elan trackpad firmware information related */
-#define ETP_I2C_PATTERN_CMD 0x0100
-#define ETP_I2C_IC_TYPE_CMD 0x0103
 #define ETP_I2C_IAP_VERSION_CMD 0x0110
-#define ETP_I2C_IC_TYPE_P0_CMD 0x0110
-#define ETP_I2C_IAP_VERSION_P0_CMD 0x0111
 #define ETP_I2C_FW_VERSION_CMD 0x0102
 #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315
 #define ETP_I2C_FW_CHECKSUM_CMD 0x030F
+#define ETP_I2C_OSM_VERSION_CMD 0x0103
 
-static void elan_get_fw_info(void)
+static int elan_get_version(int is_iap)
 {
+	elan_read_cmd(is_iap ? ETP_I2C_IAP_VERSION_CMD :
+			       ETP_I2C_FW_VERSION_CMD);
+	return le_bytes_to_int(rx_buf + 4);
+}
+
+static void elan_get_ic_page_count(void)
+{
+	elan_read_cmd(ETP_I2C_OSM_VERSION_CMD);
+
+	ic_type = rx_buf[5];
+	printf("ic_type: %02x\n", ic_type);
+
 	switch (ic_type) {
 	case 0x09:
 		fw_page_count = 768;
@@ -401,14 +408,14 @@
 	case 0x00:
 	case 0x10:
 	case 0x14:
-	case 0x15:
 		fw_page_count = 1024;
 		break;
 	default:
 		request_exit("The IC type is not supported.\n");
 	}
 
-	if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) {
+	iap_version = elan_get_version(1);
+	if (ic_type == 0x14 && iap_version >= 2) {
 		fw_page_count /= 8;
 		fw_page_size = 512;
 	} else if (ic_type >= 0x0D && iap_version >= 1) {
@@ -426,52 +433,22 @@
 	return le_bytes_to_int(rx_buf + 4);
 }
 
-static int elan_i2c_get_pattern(void)
+static uint16_t elan_get_fw_info(void)
 {
-	if (elan_read_cmd(ETP_I2C_PATTERN_CMD) != 0) {
-		return -1;
-	}
+	int fw_version = -1;
+	uint16_t iap_checksum = 0xffff;
+	uint16_t fw_checksum = 0xffff;
 
-	/*
-	 * Not all versions of firmware implement "get pattern" command. When
-	 * this command is not implemented the device will respond with 0xFFFF,
-	 * which we will treat as "old" pattern 0.
-	 */
-	int response = le16toh(*(uint16_t *)(rx_buf + I2C_RESPONSE_OFFSET));
-
-	return (response == 0xFFFF) ? 0 : rx_buf[1 + I2C_RESPONSE_OFFSET];
-}
-
-static void elan_query_product(void)
-{
-	int pattern = elan_i2c_get_pattern();
-
-	if (pattern == -1) {
-		request_exit("Failed to read ELAN device pattern");
-	}
-	printf("Pattern of ELAN touchpad: %04X\n", pattern);
-
-	if (pattern >= 0x01) {
-		if (elan_read_cmd(ETP_I2C_IC_TYPE_CMD) != 0) {
-			request_exit("Failed to read IC type");
-		}
-		ic_type = be16toh(*(uint16_t *)(rx_buf + I2C_RESPONSE_OFFSET));
-
-		if (elan_read_cmd(ETP_I2C_IAP_VERSION_CMD)) {
-			request_exit("Failed to read IAP version");
-		}
-		iap_version = rx_buf[1 + I2C_RESPONSE_OFFSET];
-	} else {
-		if (elan_read_cmd(ETP_I2C_IC_TYPE_P0_CMD) != 0) {
-			request_exit("Failed to read IC type");
-		}
-		ic_type = rx_buf[0 + I2C_RESPONSE_OFFSET];
-
-		if (elan_read_cmd(ETP_I2C_IAP_VERSION_P0_CMD)) {
-			request_exit("Failed to read IAP version");
-		}
-		iap_version = rx_buf[0 + I2C_RESPONSE_OFFSET];
-	}
+	printf("Querying device info...\n");
+	fw_checksum = elan_get_checksum(0);
+	iap_checksum = elan_get_checksum(1);
+	fw_version = elan_get_version(0);
+	iap_version = elan_get_version(1);
+	printf("IAP  version: %4x, FW  version: %4x\n", iap_version,
+	       fw_version);
+	printf("IAP checksum: %4x, FW checksum: %4x\n", iap_checksum,
+	       fw_checksum);
+	return fw_checksum;
 }
 
 /* Update preparation */
@@ -633,11 +610,10 @@
 	register_sigaction();
 
 	/*
-	 * Read pattern , then based on pattern to determine what command to
-	 * send to get IC type, IAP version, etc
+	 * Judge IC type  and get page count first.
+	 * Then check the FW file.
 	 */
-	elan_query_product();
-	elan_get_fw_info();
+	elan_get_ic_page_count();
 	fw_size = fw_page_count * fw_page_size;
 	printf("FW has %d bytes x %d pages\n", fw_page_size, fw_page_count);
 
@@ -648,6 +624,12 @@
 	if (fread(fw_data, 1, fw_size, f) != (unsigned int)fw_size)
 		request_exit("binary size mismatch, expect %d\n", fw_size);
 
+	/*
+	 * It is possible that you are not able to get firmware info. This
+	 * might due to an incomplete update last time
+	 */
+	elan_get_fw_info();
+
 	/* Trigger an I2C transaction of expecting reading of 633 bytes. */
 	if (extended_i2c_exercise) {
 		tx_buf[0] = 0x05;