writeprotect: Hook wp functions into the chip driver

Change-Id: I17a06210ec329aba337cf459d581463827182108
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72532
diff --git a/libflashprog.c b/libflashprog.c
index a7f15b5..6c2a9c9 100644
--- a/libflashprog.c
+++ b/libflashprog.c
@@ -619,15 +619,10 @@
  */
 enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
 {
-	/*
-	 * TODO: Call custom implementation if the programmer is opaque, as
-	 * direct WP operations require SPI access. In particular, linux_mtd
-	 * has its own WP operations we should use instead.
-	 */
-	if (flash->mst->buses_supported & BUS_SPI)
-		return wp_write_cfg(flash, cfg);
+	if (!flash->chip->wp_write_cfg)
+		return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
 
-	return FLASHPROG_WP_ERR_OTHER;
+	return flash->chip->wp_write_cfg(flash, cfg);
 }
 
 /**
@@ -641,15 +636,10 @@
  */
 enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
 {
-	/*
-	 * TODO: Call custom implementation if the programmer is opaque, as
-	 * direct WP operations require SPI access. In particular, linux_mtd
-	 * has its own WP operations we should use instead.
-	 */
-	if (flash->mst->buses_supported & BUS_SPI)
-		return wp_read_cfg(cfg, flash);
+	if (!flash->chip->wp_read_cfg)
+		return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
 
-	return FLASHPROG_WP_ERR_OTHER;
+	return flash->chip->wp_read_cfg(cfg, flash);
 }
 
 /**
@@ -666,16 +656,10 @@
  */
 enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
 {
-	/*
-	 * TODO: Call custom implementation if the programmer is opaque, as
-	 * direct WP operations require SPI access. We actually can't implement
-	 * this in linux_mtd right now, but we should adopt a proper generic
-	 * architechure to match the read and write functions anyway.
-	 */
-	if (flash->mst->buses_supported & BUS_SPI)
-		return wp_get_available_ranges(list, flash);
+	if (!flash->chip->wp_get_ranges)
+		return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
 
-	return FLASHPROG_WP_ERR_OTHER;
+	return flash->chip->wp_get_ranges(list, flash);
 }
 
 /**