libflashprog: Introduce new probing API flashprog_flash_probe_chip()

This new probing API acts about the same as the old one, except that
it accepts a `struct flashprog_chip *` handle instead of a chip name.
Under the hood, however, it uses our new bus probing directly.

We introduce a new function, flashprog_flash_prepare_context(), that
creates a flash context from a probed chip, and the bus it was found
on. This will be the single place that creates flash contexts in the
future.

Change-Id: I1a344aedf573d92f8f297e6eed8daadb511ebc10
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/484
diff --git a/libflashprog/chips.c b/libflashprog/chips.c
index 919d0eb..ac5cdc2 100644
--- a/libflashprog/chips.c
+++ b/libflashprog/chips.c
@@ -90,6 +90,39 @@
 	return 0;
 }
 
+/** @private */
+const struct master_common *flashprog_chip_probe(
+		const struct flashprog_programmer *flashprog,
+		const struct flashchip *chip)
+{
+	if (!chip_from_db(chip))
+		/* If not in the DB, it must be a probed `chip_entry`. */
+		return ((const struct chip_entry *)chip)->bus;
+
+	int i;
+	for (i = 0; i < registered_master_count; ++i) {
+		struct registered_master *const bus = &registered_masters[i];
+
+		if (!(bus->buses_supported & chip->bustype))
+			continue;
+
+		/* If it can't be probed, assume it's there. */
+		if (chip->id.type == ID_NONE)
+			return &bus->common;
+
+		/* We probe for a specific chip, so we can adapt the voltage early. */
+		if (bus->common.adapt_voltage &&
+		    bus->common.adapt_voltage(&bus->common, chip->voltage.min, chip->voltage.max))
+			return NULL;
+
+		flashprog_bus_probe(bus, chip);
+		if (flashprog_chip_match(bus, chip))
+			return &bus->common;
+	}
+
+	return NULL;
+}
+
 /**
  * @brief Probe for flash chips.
  *