Sun Jan 24 17:44:16 2021 UTC ()
Implement of_match_compat_data() using device_compatible_match_strlist().
Implement of_search_compatible() using device_compatible_lookup_strlist().


(thorpej)
diff -r1.45 -r1.46 src/sys/dev/ofw/ofw_subr.c

cvs diff -r1.45 -r1.46 src/sys/dev/ofw/ofw_subr.c (expand / switch to unified diff)

--- src/sys/dev/ofw/ofw_subr.c 2021/01/24 16:45:41 1.45
+++ src/sys/dev/ofw/ofw_subr.c 2021/01/24 17:44:16 1.46
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ofw_subr.c,v 1.45 2021/01/24 16:45:41 thorpej Exp $ */ 1/* $NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 thorpej Exp $ */
2 2
3/* 3/*
4 * Copyright 1998 4 * Copyright 1998
5 * Digital Equipment Corporation. All rights reserved. 5 * Digital Equipment Corporation. All rights reserved.
6 * 6 *
7 * This software is furnished under license and may be used and 7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions. 8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install, 9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary 10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby. 11 * form. No title or ownership is transferred hereby.
12 * 12 *
13 * 1) Any source code used, modified or distributed must reproduce 13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as 14 * and retain this copyright notice and list of conditions as
@@ -24,30 +24,31 @@ @@ -24,30 +24,31 @@
24 * 3) This software is provided "AS-IS" and any express or implied 24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties 25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or 26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be 27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL 28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or 29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of 30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract, 31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise, 32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage. 33 * even if advised of the possibility of such damage.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.45 2021/01/24 16:45:41 thorpej Exp $"); 37__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 thorpej Exp $");
38 38
39#include <sys/param.h> 39#include <sys/param.h>
40#include <sys/device.h> 40#include <sys/device.h>
 41#include <sys/kmem.h>
41#include <sys/systm.h> 42#include <sys/systm.h>
42#include <sys/malloc.h> 43#include <sys/malloc.h>
43#include <dev/ofw/openfirm.h> 44#include <dev/ofw/openfirm.h>
44 45
45#define OFW_MAX_STACK_BUF_SIZE 256 46#define OFW_MAX_STACK_BUF_SIZE 256
46#define OFW_PATH_BUF_SIZE 512 47#define OFW_PATH_BUF_SIZE 512
47 48
48/* 49/*
49 * int of_decode_int(p) 50 * int of_decode_int(p)
50 * 51 *
51 * This routine converts OFW encoded-int datums 52 * This routine converts OFW encoded-int datums
52 * into the integer format of the host machine. 53 * into the integer format of the host machine.
53 * 54 *
@@ -205,67 +206,93 @@ of_match_compatible(int phandle, const c @@ -205,67 +206,93 @@ of_match_compatible(int phandle, const c
205 * 206 *
206 * Return Value: 207 * Return Value:
207 * 0 if none of the strings are found in phandle's "compatibility" 208 * 0 if none of the strings are found in phandle's "compatibility"
208 * property, or a positive number based on the reverse index of the 209 * property, or a positive number based on the reverse index of the
209 * matching string in the phandle's "compatibility" property, plus 1. 210 * matching string in the phandle's "compatibility" property, plus 1.
210 * 211 *
211 * Side Effects: 212 * Side Effects:
212 * None. 213 * None.
213 */ 214 */
214int 215int
215of_match_compat_data(int phandle, 216of_match_compat_data(int phandle,
216 const struct device_compatible_entry *compat_data) 217 const struct device_compatible_entry *compat_data)
217{ 218{
218 for (; compat_data->compat != NULL; compat_data++) { 219 char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
219 const char *compat[] = { compat_data->compat, NULL }; 220 int proplen, match = 0;
220 const int match = of_match_compatible(phandle, compat); 221
221 if (match) 222 proplen = OF_getproplen(phandle, "compatible");
222 return match; 223 if (proplen <= 0) {
 224 return 0;
223 } 225 }
224 return 0; 226
 227 prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
 228
 229 if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
 230 goto out;
 231 }
 232
 233 match = device_compatible_match_strlist(prop, proplen, compat_data);
 234
 235 out:
 236 kmem_tmpbuf_free(prop, proplen, propbuf);
 237 return match;
225} 238}
226 239
227/* 240/*
228 * const struct device_compatible_entry *of_search_compatible(phandle, 241 * const struct device_compatible_entry *of_search_compatible(phandle,
229 * compat_data) 242 * compat_data)
230 * 243 *
231 * This routine searches an array of compat_data structures for a 244 * This routine searches an array of compat_data structures for a
232 * matching "compatible" entry matching the supplied OFW node. 245 * matching "compatible" entry matching the supplied OFW node.
233 * 246 *
234 * Arguments: 247 * Arguments:
235 * phandle OFW phandle of device to be checked for 248 * phandle OFW phandle of device to be checked for
236 * compatibility. 249 * compatibility.
237 * compat_data Array of possible compat entry strings and 250 * compat_data Array of possible compat entry strings and
238 * associated metadata. The last entry in the 251 * associated metadata. The last entry in the
239 * list should have a "compat" of NULL to terminate 252 * list should have a "compat" of NULL to terminate
240 * the list. 253 * the list.
241 * 254 *
242 * Return Value: 255 * Return Value:
243 * The first matching compat_data entry in the array. If no matches 256 * The first matching compat_data entry in the array. If no matches
244 * are found, the NULL is returned. 257 * are found, NULL is returned.
245 * 258 *
246 * Side Effects: 259 * Side Effects:
247 * None. 260 * None.
248 */ 261 */
249const struct device_compatible_entry * 262const struct device_compatible_entry *
250of_search_compatible(int phandle, 263of_search_compatible(int phandle,
251 const struct device_compatible_entry *compat_data) 264 const struct device_compatible_entry *compat_data)
252{ 265{
253 for (; compat_data->compat != NULL; compat_data++) { 266 char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
254 const char *compat[] = { compat_data->compat, NULL }; 267 const struct device_compatible_entry *match = NULL;
255 if (of_match_compatible(phandle, compat)) 268 int proplen;
256 return compat_data; 269
 270 proplen = OF_getproplen(phandle, "compatible");
 271 if (proplen <= 0) {
 272 return 0;
257 } 273 }
258 return NULL; 274
 275 prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
 276
 277 if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
 278 goto out;
 279 }
 280
 281 match = device_compatible_lookup_strlist(prop, proplen, compat_data);
 282
 283 out:
 284 kmem_tmpbuf_free(prop, proplen, propbuf);
 285 return match;
259} 286}
260 287
261/* 288/*
262 * int of_packagename(phandle, buf, bufsize) 289 * int of_packagename(phandle, buf, bufsize)
263 * 290 *
264 * This routine places the last component of an OFW node's name 291 * This routine places the last component of an OFW node's name
265 * into a user-provided buffer. 292 * into a user-provided buffer.
266 * 293 *
267 * It can be used during autoconfiguration to make printing of 294 * It can be used during autoconfiguration to make printing of
268 * device names more informative. 295 * device names more informative.
269 * 296 *
270 * Arguments: 297 * Arguments:
271 * phandle OFW phandle of device whose name name is 298 * phandle OFW phandle of device whose name name is