From 30570f8e89f8735a42a11f54324be371d00f7b8b Mon Sep 17 00:00:00 2001
From: Mark Kettenis <kettenis@cvs.openbsd.org>
Date: Sun, 12 Mar 2017 11:44:43 +0000
Subject: Introduce OF_getindex() API and use it to replace multiple instances
 of pretty much identical code.

ok visa@
---
 sys/dev/ofw/fdt.c         | 35 ++++++++++++++++++++--
 sys/dev/ofw/ofw_clock.c   | 74 ++++-------------------------------------------
 sys/dev/ofw/ofw_pinctrl.c | 27 ++++-------------
 sys/dev/ofw/openfirm.h    |  3 +-
 4 files changed, 45 insertions(+), 94 deletions(-)

(limited to 'sys')

diff --git a/sys/dev/ofw/fdt.c b/sys/dev/ofw/fdt.c
index b589bf1c645..d6a64499e88 100644
--- a/sys/dev/ofw/fdt.c
+++ b/sys/dev/ofw/fdt.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: fdt.c,v 1.19 2016/08/23 18:12:09 kettenis Exp $	*/
+/*	$OpenBSD: fdt.c,v 1.20 2017/03/12 11:44:42 kettenis Exp $	*/
 
 /*
  * Copyright (c) 2009 Dariusz Swiderski <sfires@sfires.net>
@@ -18,8 +18,8 @@
  */
 
 #include <sys/types.h>
-#include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/malloc.h>
 
 #include <dev/ofw/fdt.h>
 #include <dev/ofw/openfirm.h>
@@ -905,3 +905,34 @@ OF_is_compatible(int handle, const char *name)
 	return (fdt_is_compatible(node, name));
 }
 
+int
+OF_getindex(int handle, const char *entry, const char *prop)
+{
+	char *names;
+	char *name;
+	char *end;
+	int idx = 0;
+	int len;
+
+	if (entry == NULL)
+		return 0;
+
+	len = OF_getproplen(handle, (char *)prop);
+	if (len <= 0)
+		return -1;
+
+	names = malloc(len, M_TEMP, M_WAITOK);
+	OF_getprop(handle, (char *)prop, names, len);
+	end = names + len;
+	name = names;
+	while (name < end) {
+		if (strcmp(name, entry) == 0) {
+			free(names, M_TEMP, len);
+			return idx;
+		}
+		name += strlen(name) + 1;
+		idx++;
+	}
+	free(names, M_TEMP, len);
+	return -1;
+}
diff --git a/sys/dev/ofw/ofw_clock.c b/sys/dev/ofw/ofw_clock.c
index 232cf49735b..1ea57677a6e 100644
--- a/sys/dev/ofw/ofw_clock.c
+++ b/sys/dev/ofw/ofw_clock.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: ofw_clock.c,v 1.7 2016/08/27 16:50:40 kettenis Exp $	*/
+/*	$OpenBSD: ofw_clock.c,v 1.8 2017/03/12 11:44:42 kettenis Exp $	*/
 /*
  * Copyright (c) 2016 Mark Kettenis
  *
@@ -120,38 +120,6 @@ clock_next_clock(uint32_t *cells)
 	return cells + ncells + 1;
 }
 
-int
-clock_index(int node, const char *clock)
-{
-	char *names;
-	char *name;
-	char *end;
-	int idx = 0;
-	int len;
-
-	if (clock == NULL)
-		return 0;
-
-	len = OF_getproplen(node, "clock-names");
-	if (len <= 0)
-		return -1;
-
-	names = malloc(len, M_TEMP, M_WAITOK);
-	OF_getprop(node, "clock-names", names, len);
-	end = names + len;
-	name = names;
-	while (name < end) {
-		if (strcmp(name, clock) == 0) {
-			free(names, M_TEMP, len);
-			return idx;
-		}
-		name += strlen(name) + 1;
-		idx++;
-	}
-	free(names, M_TEMP, len);
-	return -1;
-}
-
 uint32_t
 clock_get_frequency_idx(int node, int idx)
 {
@@ -186,7 +154,7 @@ clock_get_frequency(int node, const char *name)
 {
 	int idx;
 
-	idx = clock_index(node, name);
+	idx = OF_getindex(node, name, "clock-names");
 	if (idx == -1)
 		return 0;
 
@@ -227,7 +195,7 @@ clock_set_frequency(int node, const char *name, uint32_t freq)
 {
 	int idx;
 
-	idx = clock_index(node, name);
+	idx = OF_getindex(node, name, "clock-names");
 	if (idx == -1)
 		return -1;
 
@@ -266,7 +234,7 @@ clock_do_enable(int node, const char *name, int on)
 {
 	int idx;
 
-	idx = clock_index(node, name);
+	idx = OF_getindex(node, name, "clock-names");
 	if (idx == -1)
 		return;
 
@@ -344,38 +312,6 @@ reset_next_reset(uint32_t *cells)
 	return cells + ncells + 1;
 }
 
-int
-reset_index(int node, const char *reset)
-{
-	char *names;
-	char *name;
-	char *end;
-	int idx = 0;
-	int len;
-
-	if (reset == NULL)
-		return 0;
-
-	len = OF_getproplen(node, "reset-names");
-	if (len <= 0)
-		return -1;
-
-	names = malloc(len, M_TEMP, M_WAITOK);
-	OF_getprop(node, "reset-names", names, len);
-	end = names + len;
-	name = names;
-	while (name < end) {
-		if (strcmp(name, reset) == 0) {
-			free(names, M_TEMP, len);
-			return idx;
-		}
-		name += strlen(name) + 1;
-		idx++;
-	}
-	free(names, M_TEMP, len);
-	return -1;
-}
-
 void
 reset_do_assert_idx(int node, int idx, int assert)
 {
@@ -408,7 +344,7 @@ reset_do_assert(int node, const char *name, int assert)
 {
 	int idx;
 
-	idx = reset_index(node, name);
+	idx = OF_getindex(node, name, "reset-names");
 	if (idx == -1)
 		return;
 
diff --git a/sys/dev/ofw/ofw_pinctrl.c b/sys/dev/ofw/ofw_pinctrl.c
index bee5454e194..75f75785ca6 100644
--- a/sys/dev/ofw/ofw_pinctrl.c
+++ b/sys/dev/ofw/ofw_pinctrl.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: ofw_pinctrl.c,v 1.1 2016/08/06 17:12:34 kettenis Exp $	*/
+/*	$OpenBSD: ofw_pinctrl.c,v 1.2 2017/03/12 11:44:42 kettenis Exp $	*/
 /*
  * Copyright (c) 2016 Mark Kettenis
  *
@@ -96,28 +96,11 @@ pinctrl_byid(int node, int id)
 int
 pinctrl_byname(int node, const char *config)
 {
-	char *names;
-	char *name;
-	char *end;
-	int id = 0;
-	int len;
+	int id;
 
-	len = OF_getproplen(node, "pinctrl-names");
-	if (len <= 0)
+	id = OF_getindex(node, config, "pinctrl-names");
+	if (id < 0)
 		return -1;
 
-	names = malloc(len, M_TEMP, M_WAITOK);
-	OF_getprop(node, "pinctrl-names", names, len);
-	end = names + len;
-	name = names;
-	while (name < end) {
-		if (strcmp(name, config) == 0) {
-			free(names, M_TEMP, len);
-			return pinctrl_byid(node, id);
-		}
-		name += strlen(name) + 1;
-		id++;
-	}
-	free(names, M_TEMP, len);
-	return -1;
+	return pinctrl_byid(node, id);
 }
diff --git a/sys/dev/ofw/openfirm.h b/sys/dev/ofw/openfirm.h
index cf3ff9c6649..39dff872259 100644
--- a/sys/dev/ofw/openfirm.h
+++ b/sys/dev/ofw/openfirm.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: openfirm.h,v 1.13 2016/07/09 12:31:05 kettenis Exp $	*/
+/*	$OpenBSD: openfirm.h,v 1.14 2017/03/12 11:44:42 kettenis Exp $	*/
 /*	$NetBSD: openfirm.h,v 1.1 1996/09/30 16:35:10 ws Exp $	*/
 
 /*
@@ -72,6 +72,7 @@ void (*OF_set_callback(void (*newfunc)(void *))) ();
 #endif
 int OF_getnodebyname(int, const char *);
 int OF_getnodebyphandle(uint32_t);
+int OF_getindex(int, const char *, const char *);
 
 /*
  * Some generic routines for OpenFirmware handling.
-- 
cgit v1.2.3