probing: Pass full struct flashchip into probes

In case we want to probe for a particular chip, we can filter the
actual probing sequences by its properties.

We'll use `struct flashchip` internally and `struct flashprog_chip`
will be used only close to the libflashprog API.  This way, we can
separate the two later again if necessary.

Change-Id: Id8b13d28fcaefee62746c9391fe86b4b3b09a428
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/441
diff --git a/edi.c b/edi.c
index 8d0819a..0f83a40 100644
--- a/edi.c
+++ b/edi.c
@@ -481,7 +481,9 @@
 		msg_perr("%s: Unable to disable EDI!\n", __func__);
 }
 
-struct found_id *probe_edi(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_edi(const struct bus_probe *probe,
+			   const struct master_common *mst,
+			   const struct flashchip *chip)
 {
 	const struct spi_master *const spi = (const struct spi_master *)mst;
 	unsigned char hwversion;
diff --git a/flashprog.c b/flashprog.c
index c1a8808..fc20045 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -617,7 +617,7 @@
 	return 0;
 }
 
-static void probe_bus(struct registered_master *const mst, const enum id_type type)
+static void probe_bus(struct registered_master *const mst, const struct flashchip *const chip)
 {
 	unsigned int least_priority, priority, i;
 	struct found_id **next_ptr;
@@ -638,10 +638,10 @@
 			if (mst->probing.probes[i].priority != priority)
 				continue;
 
-			if (type && type != mst->probing.probes[i].type)
+			if (chip && chip->id.type != mst->probing.probes[i].type)
 				continue;
 
-			*next_ptr = mst->probing.probes[i].run(&mst->probing.probes[i], &mst->common);
+			*next_ptr = mst->probing.probes[i].run(&mst->probing.probes[i], &mst->common, chip);
 			found |= !!*next_ptr;
 
 			/* walk to end in case multiple IDs were found in a single call */
@@ -657,7 +657,7 @@
 	mst->probed = true;
 }
 
-static bool chip_on_bus(struct registered_master *const mst, const struct flashprog_chip *const chip)
+static bool chip_on_bus(struct registered_master *const mst, const struct flashchip *const chip)
 {
 	static const char *const id_names[] = {
 		[ID_82802AB]	= "82802AB",
@@ -682,10 +682,10 @@
 		   If it can't be probed, assume it's there... */
 		if (chip->id.type == ID_NONE)
 			return true;
-		/* ...otherwise, limit the probing functions to its type. */
-		probe_bus(mst, chip->id.type);
+		/* ...otherwise, limit the probing sequences by its properties. */
+		probe_bus(mst, chip);
 	} else {
-		probe_bus(mst, 0);
+		probe_bus(mst, NULL);
 	}
 
 	struct found_id *found_id;
diff --git a/include/chipdrivers/edi.h b/include/chipdrivers/edi.h
index 3a1d768..4d641cc 100644
--- a/include/chipdrivers/edi.h
+++ b/include/chipdrivers/edi.h
@@ -22,8 +22,9 @@
 struct flashprog_flashctx;
 struct master_common;
 struct bus_probe;
+struct flashchip;
 
-struct found_id *probe_edi(const struct bus_probe *, const struct master_common *);
+struct found_id *probe_edi(const struct bus_probe *, const struct master_common *, const struct flashchip *chip);
 
 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);
diff --git a/include/chipdrivers/probing.h b/include/chipdrivers/probing.h
index 18eb347..9d00970 100644
--- a/include/chipdrivers/probing.h
+++ b/include/chipdrivers/probing.h
@@ -69,20 +69,20 @@
 	struct id_info_ext info;
 };
 
-struct flashprog_chip;
 struct master_common;
+struct flashchip;
 
 struct bus_probe {
 	unsigned int priority;
 	enum id_type type;
-	struct found_id *(*run)(const struct bus_probe *, const struct master_common *);
+	struct found_id *(*run)(const struct bus_probe *, const struct master_common *, const struct flashchip *);
 	void *arg;
 };
 
 struct bus_probing {
 	unsigned int probe_count;
 	const struct bus_probe *probes;
-	bool (*match)(const struct flashprog_chip *, const struct id_info_ext *);
+	bool (*match)(const struct flashchip *, const struct id_info_ext *);
 };
 
 struct flashprog_flashctx;
diff --git a/include/chipdrivers/spi.h b/include/chipdrivers/spi.h
index 547885c..94a6a1f 100644
--- a/include/chipdrivers/spi.h
+++ b/include/chipdrivers/spi.h
@@ -21,6 +21,7 @@
 struct flashprog_flashctx;
 struct master_common;
 struct bus_probe;
+struct flashchip;
 
 /* spi.c */
 int spi_aai_write(struct flashprog_flashctx *, const uint8_t *buf, unsigned int start, unsigned int len);
@@ -28,10 +29,10 @@
 int spi_chip_read(struct flashprog_flashctx *, uint8_t *buf, unsigned int start, int unsigned len);
 
 /* spi25.c */
-struct found_id *probe_spi_rdid(const struct bus_probe *, const struct master_common *);
-struct found_id *probe_spi_rems(const struct bus_probe *, const struct master_common *);
-struct found_id *probe_spi_res(const struct bus_probe *, const struct master_common *);
-struct found_id *probe_spi_at25f(const struct bus_probe *, const struct master_common *);
+struct found_id *probe_spi_rdid(const struct bus_probe *, const struct master_common *, const struct flashchip *);
+struct found_id *probe_spi_rems(const struct bus_probe *, const struct master_common *, const struct flashchip *);
+struct found_id *probe_spi_res(const struct bus_probe *, const struct master_common *, const struct flashchip *);
+struct found_id *probe_spi_at25f(const struct bus_probe *, const struct master_common *, const struct flashchip *);
 
 int spi_simple_write_cmd(struct flashprog_flashctx *, uint8_t op, unsigned int poll_delay);
 int spi_write_enable(struct flashprog_flashctx *);
@@ -119,7 +120,7 @@
 int spi_erase_at45cs_sector(struct flashprog_flashctx *, unsigned int addr, unsigned int blocklen);
 
 /* spi95.c */
-struct found_id *probe_spi_st95(const struct bus_probe *, const struct master_common *);
+struct found_id *probe_spi_st95(const struct bus_probe *, const struct master_common *, const struct flashchip *);
 int spi_block_erase_emulation(struct flashprog_flashctx *, unsigned int addr, unsigned int blocklen);
 
 /* writeprotect_ranges.c */
@@ -130,7 +131,7 @@
 void decode_range_spi25_2x_block(size_t *start, size_t *len, const struct wp_bits *, size_t chip_len);
 
 /* sfdp.c */
-struct found_id *probe_spi_sfdp(const struct bus_probe *, const struct master_common *);
+struct found_id *probe_spi_sfdp(const struct bus_probe *, const struct master_common *, const struct flashchip *);
 int spi_prepare_sfdp(struct flashprog_flashctx *, enum preparation_steps);
 
 #endif /* !__CHIPDRIVERS_SPI_H__ */
diff --git a/include/flash.h b/include/flash.h
index 0f14037..aca1b77 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -244,8 +244,7 @@
 	PREPARE_FULL,
 };
 
-#define flashchip flashprog_chip
-struct flashprog_chip {
+struct flashchip {
 	const char *vendor;
 	const char *name;
 
diff --git a/parallel.c b/parallel.c
index b113670..5446e1a 100644
--- a/parallel.c
+++ b/parallel.c
@@ -80,7 +80,7 @@
     /* prio. type		function		function argument */
 };
 
-static bool memory_probe_match(const struct flashprog_chip *chip, const struct id_info_ext *found)
+static bool memory_probe_match(const struct flashchip *chip, const struct id_info_ext *found)
 {
 	const struct memory_chip_info *const probe_info = found->ext;
 
diff --git a/sfdp.c b/sfdp.c
index 45303ae..b8f3093 100644
--- a/sfdp.c
+++ b/sfdp.c
@@ -473,7 +473,9 @@
 	return ret;
 }
 
-struct found_id *probe_spi_sfdp(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_sfdp(const struct bus_probe *probe,
+				const struct master_common *mst,
+				const struct flashchip *chip)
 {
 	uint8_t buf[8];
 	uint32_t tmp32;
diff --git a/spi25.c b/spi25.c
index f45f9a2..a628770 100644
--- a/spi25.c
+++ b/spi25.c
@@ -96,7 +96,9 @@
 	return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
 }
 
-struct found_id *probe_spi_rdid(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_rdid(const struct bus_probe *probe,
+				const struct master_common *mst,
+				const struct flashchip *chip)
 {
 	const struct spi_master *const spi = (const struct spi_master *)mst;
 	unsigned char readarr[4];
@@ -147,7 +149,9 @@
 	return found;
 }
 
-struct found_id *probe_spi_rems(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_rems(const struct bus_probe *probe,
+				const struct master_common *mst,
+				const struct flashchip *chip)
 {
 	const struct spi_master *const spi = (const struct spi_master *)mst;
 	unsigned char readarr[JEDEC_REMS_INSIZE];
@@ -173,7 +177,9 @@
 	return found;
 }
 
-struct found_id *probe_spi_res(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_res(const struct bus_probe *probe,
+			       const struct master_common *mst,
+			       const struct flashchip *chip)
 {
 	const struct spi_master *const spi = (const struct spi_master *)mst;
 	const unsigned int res_len = probe->type == ID_SPI_RES3 ? 3 :
@@ -229,7 +235,9 @@
 }
 
 /* Only used for some Atmel chips. */
-struct found_id *probe_spi_at25f(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_at25f(const struct bus_probe *probe,
+				 const struct master_common *mst,
+				 const struct flashchip *chip)
 {
 	static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
 	const struct spi_master *const spi = (const struct spi_master *)mst;
diff --git a/spi95.c b/spi95.c
index 1ffa00f..70a2074 100644
--- a/spi95.c
+++ b/spi95.c
@@ -25,7 +25,9 @@
 #include "spi.h"
 
 /* For ST95XXX chips which have RDID */
-struct found_id *probe_spi_st95(const struct bus_probe *probe, const struct master_common *mst)
+struct found_id *probe_spi_st95(const struct bus_probe *probe,
+				const struct master_common *mst,
+				const struct flashchip *chip)
 {
 	/*
 	 * ST_M95_RDID_OUTSIZE depends on size of the flash and