Module Name:    src
Committed By:   thorpej
Date:           Mon Jan 18 15:28:21 UTC 2021

Modified Files:
        src/sys/dev/i2c: at24cxx.c axppmic.c ds1307.c fan53555.c i2c.c i2cvar.h
            m41st84.c pcagpio.c pcai2cmux.c rkpmic.c
        src/sys/dev/spi: spi.c
        src/sys/kern: subr_autoconf.c
        src/sys/sys: device.h

Log Message:
Change the device_compatible_match() function to only perform the match.
Introduce a device_compatible_lookup() function to return an entry based
on the same matching criteria (a'la of_search_compatible()).

Update iic_compatible_match() to reflect the above change, and introduce
iic_compatible_lookup().  This pattern is less awkward to use.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/sys/dev/i2c/at24cxx.c
cvs rdiff -u -r1.30 -r1.31 src/sys/dev/i2c/axppmic.c
cvs rdiff -u -r1.35 -r1.36 src/sys/dev/i2c/ds1307.c
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/i2c/fan53555.c
cvs rdiff -u -r1.75 -r1.76 src/sys/dev/i2c/i2c.c
cvs rdiff -u -r1.21 -r1.22 src/sys/dev/i2c/i2cvar.h
cvs rdiff -u -r1.28 -r1.29 src/sys/dev/i2c/m41st84.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/i2c/pcagpio.c
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/i2c/pcai2cmux.c
cvs rdiff -u -r1.9 -r1.10 src/sys/dev/i2c/rkpmic.c
cvs rdiff -u -r1.15 -r1.16 src/sys/dev/spi/spi.c
cvs rdiff -u -r1.274 -r1.275 src/sys/kern/subr_autoconf.c
cvs rdiff -u -r1.160 -r1.161 src/sys/sys/device.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/i2c/at24cxx.c
diff -u src/sys/dev/i2c/at24cxx.c:1.37 src/sys/dev/i2c/at24cxx.c:1.38
--- src/sys/dev/i2c/at24cxx.c:1.37	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/at24cxx.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: at24cxx.c,v 1.37 2021/01/17 21:56:20 thorpej Exp $	*/
+/*	$NetBSD: at24cxx.c,v 1.38 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: at24cxx.c,v 1.37 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: at24cxx.c,v 1.38 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -182,7 +182,7 @@ seeprom_attach(device_t parent, device_t
 		sc->sc_size = (device_cfdata(self)->cf_flags << 7);
 
 	if (sc->sc_size <= 0 && ia->ia_ncompat > 0) {
-		if (iic_compatible_match(ia, compat_data, &dce))
+		if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
 			sc->sc_size = dce->value;
 	}
 

Index: src/sys/dev/i2c/axppmic.c
diff -u src/sys/dev/i2c/axppmic.c:1.30 src/sys/dev/i2c/axppmic.c:1.31
--- src/sys/dev/i2c/axppmic.c:1.30	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/axppmic.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: axppmic.c,v 1.30 2021/01/17 21:56:20 thorpej Exp $ */
+/* $NetBSD: axppmic.c,v 1.31 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014-2018 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.30 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.31 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -949,7 +949,7 @@ axppmic_attach(device_t parent, device_t
 	uint8_t irq_mask, val;
 	int error;
 
-	(void) iic_compatible_match(ia, compat_data, &dce);
+	dce = iic_compatible_lookup(ia, compat_data);
 	KASSERT(dce != NULL);
 	c = dce->data;
 

Index: src/sys/dev/i2c/ds1307.c
diff -u src/sys/dev/i2c/ds1307.c:1.35 src/sys/dev/i2c/ds1307.c:1.36
--- src/sys/dev/i2c/ds1307.c:1.35	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/ds1307.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ds1307.c,v 1.35 2021/01/17 21:56:20 thorpej Exp $	*/
+/*	$NetBSD: ds1307.c,v 1.36 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ds1307.c,v 1.35 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ds1307.c,v 1.36 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -255,7 +255,7 @@ dsrtc_model_by_compat(const struct i2c_a
 	const struct dsrtc_model *dm = NULL;
 	const struct device_compatible_entry *dce;
 
-	if (iic_compatible_match(ia, compat_data, &dce))
+	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
 		dm = dce->data;
 
 	return dm;

Index: src/sys/dev/i2c/fan53555.c
diff -u src/sys/dev/i2c/fan53555.c:1.6 src/sys/dev/i2c/fan53555.c:1.7
--- src/sys/dev/i2c/fan53555.c:1.6	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/fan53555.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: fan53555.c,v 1.6 2021/01/17 21:56:20 thorpej Exp $ */
+/* $NetBSD: fan53555.c,v 1.7 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.6 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.7 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -310,7 +310,7 @@ fan53555_attach(device_t parent, device_
 	sc->sc_addr = ia->ia_addr;
 	sc->sc_phandle = ia->ia_cookie;
 
-	iic_compatible_match(ia, compat_data, &compat);
+	compat = iic_compatible_lookup(ia, compat_data);
 	KASSERT(compat != NULL);
 
 	if (fan53555_init(sc, compat->value) != 0)

Index: src/sys/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.75 src/sys/dev/i2c/i2c.c:1.76
--- src/sys/dev/i2c/i2c.c:1.75	Tue Jul  7 16:14:23 2020
+++ src/sys/dev/i2c/i2c.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.75 2020/07/07 16:14:23 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.76 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -40,7 +40,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.75 2020/07/07 16:14:23 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.76 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -687,13 +687,12 @@ iic_fill_compat(struct i2c_attach_args *
  */
 int
 iic_compatible_match(const struct i2c_attach_args *ia,
-		     const struct device_compatible_entry *compats,
-		     const struct device_compatible_entry **matching_entryp)
+		     const struct device_compatible_entry *compats)
 {
 	int match_result;
 
 	match_result = device_compatible_match(ia->ia_compat, ia->ia_ncompat,
-					       compats, matching_entryp);
+					       compats);
 	if (match_result) {
 		match_result =
 		    MIN(I2C_MATCH_DIRECT_COMPATIBLE + match_result - 1,
@@ -704,6 +703,19 @@ iic_compatible_match(const struct i2c_at
 }
 
 /*
+ * iic_compatible_lookup --
+ *	Look the compatible entry that matches one of the driver's
+ *	"compatible" strings.  The first match is returned.
+ */
+const struct device_compatible_entry *
+iic_compatible_lookup(const struct i2c_attach_args *ia,
+		      const struct device_compatible_entry *compats)
+{
+	return device_compatible_lookup(ia->ia_compat, ia->ia_ncompat,
+					compats);
+}
+
+/*
  * iic_use_direct_match --
  *	Helper for direct-config of i2c.  Returns true if this is
  *	a direct-config situation, along with with match result.
@@ -724,7 +736,7 @@ iic_use_direct_match(const struct i2c_at
 	}
 
 	if (ia->ia_ncompat > 0 && ia->ia_compat != NULL) {
-		*match_resultp = iic_compatible_match(ia, compats, NULL);
+		*match_resultp = iic_compatible_match(ia, compats);
 		return true;
 	}
 

Index: src/sys/dev/i2c/i2cvar.h
diff -u src/sys/dev/i2c/i2cvar.h:1.21 src/sys/dev/i2c/i2cvar.h:1.22
--- src/sys/dev/i2c/i2cvar.h:1.21	Tue Dec 29 00:26:51 2020
+++ src/sys/dev/i2c/i2cvar.h	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cvar.h,v 1.21 2020/12/29 00:26:51 thorpej Exp $	*/
+/*	$NetBSD: i2cvar.h,v 1.22 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -171,10 +171,12 @@ void	iic_tag_fini(i2c_tag_t);
  * API presented to i2c devices.
  */
 int	iic_compatible_match(const struct i2c_attach_args *,
-			     const struct device_compatible_entry *,
-			     const struct device_compatible_entry **);
+			     const struct device_compatible_entry *);
 bool	iic_use_direct_match(const struct i2c_attach_args *, const cfdata_t,
 			     const struct device_compatible_entry *, int *);
+const struct device_compatible_entry *
+	iic_compatible_lookup(const struct i2c_attach_args *,
+			      const struct device_compatible_entry *);
 
 /*
  * Constants to indicate the quality of a match made by a driver's

Index: src/sys/dev/i2c/m41st84.c
diff -u src/sys/dev/i2c/m41st84.c:1.28 src/sys/dev/i2c/m41st84.c:1.29
--- src/sys/dev/i2c/m41st84.c:1.28	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/m41st84.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: m41st84.c,v 1.28 2021/01/17 21:56:20 thorpej Exp $	*/
+/*	$NetBSD: m41st84.c,v 1.29 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: m41st84.c,v 1.28 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: m41st84.c,v 1.29 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -140,7 +140,7 @@ strtc_model_by_number(u_int model)
 		return &m41t80_model;
 	
 	for (dce = compat_data; dce->compat != NULL; dce++) {
-		sm = (void *)dce->data;
+		sm = dce->data;
 		if (sm->sm_model == model)
 			return sm;
 	}
@@ -153,8 +153,8 @@ strtc_model_by_compat(const struct i2c_a
 	const struct device_compatible_entry *dce;
 	const struct strtc_model *sm = NULL;
 
-	if (iic_compatible_match(ia, compat_data, &dce))
-		sm = (void *)dce->data;
+	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
+		sm = dce->data;
 	
 	return sm;
 }

Index: src/sys/dev/i2c/pcagpio.c
diff -u src/sys/dev/i2c/pcagpio.c:1.7 src/sys/dev/i2c/pcagpio.c:1.8
--- src/sys/dev/i2c/pcagpio.c:1.7	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/pcagpio.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: pcagpio.c,v 1.7 2021/01/17 21:56:20 thorpej Exp $ */
+/* $NetBSD: pcagpio.c,v 1.8 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2020 Michael Lorenz
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.7 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.8 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -157,7 +157,7 @@ pcagpio_attach(device_t parent, device_t
 
 	aprint_naive("\n");
 	sc->sc_is_16bit = 0;
-	if (iic_compatible_match(ia, compat_data, &dce))
+	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
 		sc->sc_is_16bit = dce->value;
 
 	aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");

Index: src/sys/dev/i2c/pcai2cmux.c
diff -u src/sys/dev/i2c/pcai2cmux.c:1.2 src/sys/dev/i2c/pcai2cmux.c:1.3
--- src/sys/dev/i2c/pcai2cmux.c:1.2	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/pcai2cmux.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcai2cmux.c,v 1.2 2021/01/17 21:56:20 thorpej Exp $	*/
+/*	$NetBSD: pcai2cmux.c,v 1.3 2021/01/18 15:28:21 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.2 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.3 2021/01/18 15:28:21 thorpej Exp $");
 
 /*
  * Driver for NXP PCA954x / PCA984x I2C switches and multiplexers.
@@ -276,7 +276,7 @@ pcaiicmux_type_by_compat(const struct i2
 	const struct pcaiicmux_type *type = NULL;
 	const struct device_compatible_entry *dce;
 
-	if (iic_compatible_match(ia, compat_data, &dce))
+	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
 		type = dce->data;
 
 	return type;

Index: src/sys/dev/i2c/rkpmic.c
diff -u src/sys/dev/i2c/rkpmic.c:1.9 src/sys/dev/i2c/rkpmic.c:1.10
--- src/sys/dev/i2c/rkpmic.c:1.9	Sun Jan 17 21:56:20 2021
+++ src/sys/dev/i2c/rkpmic.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: rkpmic.c,v 1.9 2021/01/17 21:56:20 thorpej Exp $ */
+/* $NetBSD: rkpmic.c,v 1.10 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rkpmic.c,v 1.9 2021/01/17 21:56:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rkpmic.c,v 1.10 2021/01/18 15:28:21 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -467,7 +467,8 @@ rkpmic_attach(device_t parent, device_t 
 	int child, regulators;
 	u_int chipid, n;
 
-	iic_compatible_match(ia, compat_data, &entry);
+	entry = iic_compatible_lookup(ia, compat_data);
+	KASSERT(entry != NULL);
 
 	sc->sc_dev = self;
 	sc->sc_i2c = ia->ia_tag;

Index: src/sys/dev/spi/spi.c
diff -u src/sys/dev/spi/spi.c:1.15 src/sys/dev/spi/spi.c:1.16
--- src/sys/dev/spi/spi.c:1.15	Tue Aug  4 13:20:45 2020
+++ src/sys/dev/spi/spi.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spi.c,v 1.15 2020/08/04 13:20:45 kardel Exp $ */
+/* $NetBSD: spi.c,v 1.16 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.15 2020/08/04 13:20:45 kardel Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.16 2021/01/18 15:28:21 thorpej Exp $");
 
 #include "locators.h"
 
@@ -266,7 +266,7 @@ spi_compatible_match(const struct spi_at
 {
 	if (sa->sa_ncompat > 0)
 		return device_compatible_match(sa->sa_compat, sa->sa_ncompat,
-					       compats, NULL);
+					       compats);
 
 	return 1;
 }

Index: src/sys/kern/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.274 src/sys/kern/subr_autoconf.c:1.275
--- src/sys/kern/subr_autoconf.c:1.274	Sat Oct  3 22:32:50 2020
+++ src/sys/kern/subr_autoconf.c	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.274 2020/10/03 22:32:50 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.275 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.274 2020/10/03 22:32:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.275 2021/01/18 15:28:21 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -2332,10 +2332,11 @@ device_find_by_driver_unit(const char *n
  *	a weighted match result, and optionally the matching
  *	entry.
  */
-int
-device_compatible_match(const char **device_compats, int ndevice_compats,
-			const struct device_compatible_entry *driver_compats,
-			const struct device_compatible_entry **matching_entryp)
+static int
+device_compatible_match_internal(const char **device_compats,
+    int ndevice_compats,
+    const struct device_compatible_entry *driver_compats,
+    const struct device_compatible_entry **matching_entryp)
 {
 	const struct device_compatible_entry *dce = NULL;
 	int i, match_weight;
@@ -2363,6 +2364,33 @@ device_compatible_match(const char **dev
 	return 0;
 }
 
+int
+device_compatible_match(const char **device_compats, int ndevice_compats,
+			const struct device_compatible_entry *driver_compats)
+{
+	return device_compatible_match_internal(device_compats, ndevice_compats,
+						driver_compats, NULL);
+}
+
+/*
+ * device_compatible_lookup:
+ *
+ *	Look up and return the device_compatible_entry, using the
+ *	same matching criteria used by device_compatible_match().
+ */
+const struct device_compatible_entry *
+device_compatible_lookup(const char **device_compats, int ndevice_compats,
+			 const struct device_compatible_entry *driver_compats)
+{
+	const struct device_compatible_entry *dce;
+
+	if (device_compatible_match_internal(device_compats, ndevice_compats,
+					     driver_compats, &dce)) {
+		return dce;
+	}
+	return NULL;
+}
+
 /*
  * Power management related functions.
  */

Index: src/sys/sys/device.h
diff -u src/sys/sys/device.h:1.160 src/sys/sys/device.h:1.161
--- src/sys/sys/device.h:1.160	Sun Jan 17 21:56:20 2021
+++ src/sys/sys/device.h	Mon Jan 18 15:28:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: device.h,v 1.160 2021/01/17 21:56:20 thorpej Exp $ */
+/* $NetBSD: device.h,v 1.161 2021/01/18 15:28:21 thorpej Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -544,8 +544,10 @@ device_t	device_find_by_xname(const char
 device_t	device_find_by_driver_unit(const char *, int);
 
 int		device_compatible_match(const char **, int,
-				const struct device_compatible_entry *,
-				const struct device_compatible_entry **);
+				const struct device_compatible_entry *);
+const struct device_compatible_entry *
+		device_compatible_lookup(const char **, int,
+				const struct device_compatible_entry *);
 
 bool		device_pmf_is_registered(device_t);
 

Reply via email to