A recurring pattern in device trees is that there is a "xxx-names"
property that allows you to lookup an entry in the corresponding "xxx"
property by names. Examples that we already use in our tree are
"clock-names", "reset-names" and "pinctrl-names". There is duplicated
code to do this for all three cases, and I just found myself writing
similar code yet again. So here is a diff that adds a new generic
openfirmware.h API that does such a lookup. It accepts a node handle,
the name of the entry you're looking up and the name of the propery
The casts are (unfortunately) necessary because the API dealing with
properties isn't const-correct.
ok?
Index: dev/ofw/fdt.c
===================================================================
RCS file: /cvs/src/sys/dev/ofw/fdt.c,v
retrieving revision 1.19
diff -u -p -r1.19 fdt.c
--- dev/ofw/fdt.c 23 Aug 2016 18:12:09 -0000 1.19
+++ dev/ofw/fdt.c 11 Mar 2017 13:32:31 -0000
@@ -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
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;
+}
Index: dev/ofw/ofw_clock.c
===================================================================
RCS file: /cvs/src/sys/dev/ofw/ofw_clock.c,v
retrieving revision 1.7
diff -u -p -r1.7 ofw_clock.c
--- dev/ofw/ofw_clock.c 27 Aug 2016 16:50:40 -0000 1.7
+++ dev/ofw/ofw_clock.c 11 Mar 2017 13:32:31 -0000
@@ -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
{
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
{
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 *na
{
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 *na
{
int idx;
- idx = reset_index(node, name);
+ idx = OF_getindex(node, name, "reset-names");
if (idx == -1)
return;
Index: dev/ofw/ofw_pinctrl.c
===================================================================
RCS file: /cvs/src/sys/dev/ofw/ofw_pinctrl.c,v
retrieving revision 1.1
diff -u -p -r1.1 ofw_pinctrl.c
--- dev/ofw/ofw_pinctrl.c 6 Aug 2016 17:12:34 -0000 1.1
+++ dev/ofw/ofw_pinctrl.c 11 Mar 2017 13:32:31 -0000
@@ -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);
}
Index: dev/ofw/openfirm.h
===================================================================
RCS file: /cvs/src/sys/dev/ofw/openfirm.h,v
retrieving revision 1.13
diff -u -p -r1.13 openfirm.h
--- dev/ofw/openfirm.h 9 Jul 2016 12:31:05 -0000 1.13
+++ dev/ofw/openfirm.h 11 Mar 2017 13:32:31 -0000
@@ -72,6 +72,7 @@ void (*OF_set_callback(void (*newfunc)(v
#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.