edi: Turn edi_chip_probe() into a bus probing function

Let our common bus-probing infrastructure handle the ID comparison.
This also makes the `flashchips' entry (KB9012) an actual chip entry
that carries its identification.

Change-Id: I9533ece2b1337281ea70cb2e3be7a74353a4a758
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/424
diff --git a/edi.c b/edi.c
index 8adba16..8d0819a 100644
--- a/edi.c
+++ b/edi.c
@@ -24,11 +24,6 @@
 
 static unsigned int edi_read_buffer_length = EDI_READ_BUFFER_LENGTH_DEFAULT;
 
-static const struct ene_chip ene_kb9012 = {
-	.hwversion = ENE_KB9012_HWVERSION,
-	.ediid = ENE_KB9012_EDIID,
-};
-
 static void edi_write_cmd(unsigned char *cmd, unsigned short address, unsigned char data)
 {
 	cmd[0] = EDI_WRITE; /* EDI write command. */
@@ -146,7 +141,7 @@
 	return 0;
 }
 
-static int edi_chip_probe(const struct spi_master *spi, const struct ene_chip *chip)
+static struct found_id *edi_chip_probe(const struct spi_master *spi)
 {
 	unsigned char hwversion;
 	unsigned char ediid;
@@ -155,21 +150,30 @@
 	rc = edi_read(spi, ENE_EC_HWVERSION, &hwversion);
 	if (rc < 0) {
 		msg_cdbg("%s: reading hwversion failed\n", __func__);
-		return 0;
+		return NULL;
 	}
 
 	rc = edi_read(spi, ENE_EC_EDIID, &ediid);
 	if (rc < 0) {
 		msg_cdbg("%s: reading ediid failed\n", __func__);
-		return 0;
+		return NULL;
 	}
 
+	struct found_id *const found = calloc(1, sizeof(*found));
+	if (!found) {
+		msg_cerr("Out of memory!\n");
+		return NULL;
+	}
+
+	struct id_info *const id = &found->info.id;
+
+	id->hwversion	= hwversion;
+	id->model	= ediid;
+	id->type	= ID_EDI;
+
 	msg_cdbg("%s: hwversion 0x%02x, ediid 0x%02x\n", __func__, hwversion, ediid);
 
-	if (chip->hwversion == hwversion && chip->ediid == ediid)
-		return 1;
-
-	return 0;
+	return found;
 }
 
 static int edi_spi_enable(const struct spi_master *spi)
@@ -477,9 +481,9 @@
 		msg_perr("%s: Unable to disable EDI!\n", __func__);
 }
 
-int edi_probe_kb9012(struct flashctx *flash)
+struct found_id *probe_edi(const struct bus_probe *probe, const struct master_common *mst)
 {
-	const struct spi_master *const spi = flash->mst.spi;
+	const struct spi_master *const spi = (const struct spi_master *)mst;
 	unsigned char hwversion;
 
 	/*
@@ -493,7 +497,7 @@
 	 */
 	edi_read(spi, ENE_EC_HWVERSION, &hwversion);
 
-	return edi_chip_probe(spi, &ene_kb9012);
+	return edi_chip_probe(spi);
 }
 
 int edi_prepare(struct flashctx *flash, enum preparation_steps step)
diff --git a/flashchips.c b/flashchips.c
index 02aaf88..bd07239 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -18,6 +18,7 @@
  * GNU General Public License for more details.
  */
 
+#include "ene.h"
 #include "flash.h"
 #include "flashchips.h"
 #include "chipdrivers.h"
@@ -4187,12 +4188,14 @@
 		.name		= "KB9012 (EDI)",
 		.bustype	= BUS_SPI,
 		.id.type	= ID_EDI,
+		.id.hwversion	= ENE_KB9012_HWVERSION,
+		.id.model	= ENE_KB9012_EDIID,
 		.total_size	= 128,
 		.page_size	= 128,
 		.feature_bits	= FEATURE_ERASED_ZERO,
 		.tested		= TEST_OK_PREW,
 		.spi_cmd_set	= SPI_EDI,
-		.probe		= edi_probe_kb9012,
+		.probe		= probe_buses,
 		.probe_timing	= TIMING_ZERO,
 		.block_erasers	=
 		{
diff --git a/include/chipdrivers/edi.h b/include/chipdrivers/edi.h
index 48f24be..3a1d768 100644
--- a/include/chipdrivers/edi.h
+++ b/include/chipdrivers/edi.h
@@ -20,11 +20,14 @@
 #include <stdint.h>
 
 struct flashprog_flashctx;
+struct master_common;
+struct bus_probe;
+
+struct found_id *probe_edi(const struct bus_probe *, const struct master_common *);
 
 int edi_chip_block_erase(struct flashprog_flashctx *, unsigned int page, unsigned int size);
 int edi_chip_write(struct flashprog_flashctx *, const uint8_t *buf, unsigned int start, unsigned int len);
 int edi_chip_read(struct flashprog_flashctx *, uint8_t *buf, unsigned int start, unsigned int len);
-int edi_probe_kb9012(struct flashprog_flashctx *);
 int edi_prepare(struct flashprog_flashctx *, enum preparation_steps);
 
 #endif /* !__CHIPDRIVERS_EDI_H__ */
diff --git a/include/chipdrivers/probing.h b/include/chipdrivers/probing.h
index 561d6ef..18eb347 100644
--- a/include/chipdrivers/probing.h
+++ b/include/chipdrivers/probing.h
@@ -49,6 +49,7 @@
 struct id_info {
 	union {
 		uint32_t manufacture;
+		uint32_t hwversion;
 		uint32_t id1;
 	};
 	union {
diff --git a/include/ene.h b/include/ene.h
index e03e49b..14ca0bb 100644
--- a/include/ene.h
+++ b/include/ene.h
@@ -43,9 +43,4 @@
 #define ENE_KB9012_HWVERSION		0xc3
 #define ENE_KB9012_EDIID		0x04
 
-struct ene_chip {
-	unsigned char hwversion;
-	unsigned char ediid;
-};
-
 #endif
diff --git a/spi.c b/spi.c
index 7cacca3..15809c6 100644
--- a/spi.c
+++ b/spi.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include "flash.h"
 #include "flashchips.h"
+#include "chipdrivers/edi.h"
 #include "chipdrivers/spi.h"
 #include "chipdrivers/probing.h"
 #include "programmer.h"
@@ -173,6 +174,7 @@
 	{ 0, ID_SPI_SFDP,	probe_spi_sfdp,		NULL },
 	{ 1, ID_SPI_ST95,	probe_spi_st95,		(void *)(uintptr_t)3 },
 	{ 1, ID_SPI_ST95,	probe_spi_st95,		(void *)(uintptr_t)2 },
+	{ 2, ID_EDI,		probe_edi,		NULL },
 };
 
 static bool spi_probe_match(const struct flashchip *chip, const struct id_info_ext *found)