tree/: Drop const from opaque data ptr in master definitions [alt]

The opaque data pointer need not necessarily have constant
data for the life-time of the specific master. This is because
the data field purpose is for the master to use as it sees fit
for managing its own internal state and therefore we should not
constrain this as being RO data at init time.

flashrom-stable:
* Applied bitbang_spi part, now that we can test it.
* Added OOM check.
* Use new shutdown API right away.

Change-Id: I6c3cf561313de1de4e3caec8732137a8c23bf1e6
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/49131
Original-Reviewed-by: Sam McNally <sammc@google.com>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/73266
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/bitbang_spi.c b/bitbang_spi.c
index 850ca32..91f48a9 100644
--- a/bitbang_spi.c
+++ b/bitbang_spi.c
@@ -21,6 +21,10 @@
 #include "programmer.h"
 #include "spi.h"
 
+struct bitbang_spi_master_data {
+	const struct bitbang_spi_master *mst;
+};
+
 /* Note that CS# is active low, so val=0 means the chip is active. */
 static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val)
 {
@@ -68,6 +72,7 @@
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr);
+static int bitbang_spi_shutdown(void *data);
 
 static const struct spi_master spi_master_bitbang = {
 	.features	= SPI_MASTER_4BA,
@@ -77,16 +82,16 @@
 	.multicommand	= default_spi_send_multicommand,
 	.read		= default_spi_read,
 	.write_256	= default_spi_write_256,
+	.shutdown	= bitbang_spi_shutdown,
 	.probe_opcode	= default_spi_probe_opcode,
 };
 
-#if 0 // until it is needed
-static int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
+static int bitbang_spi_shutdown(void *data)
 {
 	/* FIXME: Run bitbang_spi_release_bus here or per command? */
+	free(data);
 	return 0;
 }
-#endif
 
 int register_spi_bitbang_master(const struct bitbang_spi_master *master)
 {
@@ -101,9 +106,14 @@
 		return ERROR_FLASHROM_BUG;
 	}
 
-	/* Cast away `const`, but local code must ensure it's still treated as such. */
-	mst.data = (struct bitbang_spi_master *)master;
-	register_spi_master(&mst, NULL);
+	struct bitbang_spi_master_data *data = calloc(1, sizeof(struct bitbang_spi_master_data));
+	if (!data) {
+		msg_perr("Out of memory!\n");
+		return ERROR_OOM;
+	}
+
+	data->mst = master;
+	register_spi_master(&mst, data);
 
 	/* Only mess with the bus if we're sure nobody else uses it. */
 	bitbang_spi_request_bus(master);
@@ -152,7 +162,8 @@
 				    unsigned char *readarr)
 {
 	unsigned int i;
-	const struct bitbang_spi_master *master = flash->mst->spi.data;
+	const struct bitbang_spi_master_data *data = flash->mst->spi.data;
+	const struct bitbang_spi_master *master = data->mst;
 
 	/* FIXME: Run bitbang_spi_request_bus here or in programmer init?
 	 * Requesting and releasing the SPI bus is handled in here to allow the