From owner-freebsd-embedded@FreeBSD.ORG Wed Oct 23 14:45:31 2013 Return-Path: Delivered-To: freebsd-embedded@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 1D5C57DB for ; Wed, 23 Oct 2013 14:45:31 +0000 (UTC) (envelope-from imp@bsdimp.com) Received: from mail-ie0-f170.google.com (mail-ie0-f170.google.com [209.85.223.170]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id DF5A12518 for ; Wed, 23 Oct 2013 14:45:30 +0000 (UTC) Received: by mail-ie0-f170.google.com with SMTP id at1so1499104iec.29 for ; Wed, 23 Oct 2013 07:45:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:subject:mime-version:content-type:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=dipjXA3s0mAoswcotZz+zGdQBrtahG/twWeQh9vWhWc=; b=kcnf1rpLigBcFC0lOxBzRLHYYAsPh84x4XOTbsg7Qe+SKmCv7EAd+eNpPyV7RQzEfv ZfoQ9vlzMjXOBXUdWa4SK8oIBq+2WeMajaHadJWaAlLncXVi8S+HGmAPJ/tXzVW4OcPy St+8EJy9EUNk4mR/Ueph2x2IrhLGNIhqfWZeQQvmvnA5bnL3Mo9E5d0tN1+K44cWMVMm tvI/BGM2ZHdFfS6/UcH0mnkpe5aFXzOeXMC2XmqVC8kcQiAC/xYZlEZ47Rw5ReNMCynj dZAkORkpD6MvL7fQZRf6oVW9AN4al7PVeInuYOSWykNZbM/VQz08VGrDG2ni6jByYzDN axLw== X-Gm-Message-State: ALoCoQmI6FJkt4cFo9vBKCTd+Ym6DY6IiULHfuEZurKZfTVlcZJQN3YKNwBHjUOcKQvSMpdHWBlC X-Received: by 10.50.26.36 with SMTP id i4mr1007300igg.33.1382539530263; Wed, 23 Oct 2013 07:45:30 -0700 (PDT) Received: from 53.imp.bsdimp.com (50-78-194-198-static.hfc.comcastbusiness.net. [50.78.194.198]) by mx.google.com with ESMTPSA id cl4sm8037510igc.1.2013.10.23.07.45.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 23 Oct 2013 07:45:29 -0700 (PDT) Sender: Warner Losh Subject: Re: new ofw_search_compatible() Mime-Version: 1.0 (Apple Message framework v1085) Content-Type: text/plain; charset=us-ascii From: Warner Losh In-Reply-To: <1382539150.92499.206.camel@revolution.hippie.lan> Date: Wed, 23 Oct 2013 08:45:28 -0600 Content-Transfer-Encoding: quoted-printable Message-Id: <38F33DD0-4D30-438A-8380-D2354444420C@bsdimp.com> References: <1382539150.92499.206.camel@revolution.hippie.lan> To: Ian Lepore X-Mailer: Apple Mail (2.1085) Cc: freebsd-embedded@FreeBSD.org X-BeenThere: freebsd-embedded@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Dedicated and Embedded Systems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Oct 2013 14:45:31 -0000 I like this... For PC Card I took a slightly different tact where I passed in the size = of the element and returned a pointer to the element. Each element = started with a struct that described what to match. Worked well there, = but most of the time all that was looked up was a simple int. A more = general solution, but maybe not worth the extra hair, unless you combine = it with a probe routine that is 100% driven from tables (which the pc = card client drivers were). The only other nit in the interface is that it requires a NULL = terminator. I'd prefer a count of elements, but I could go either way. Warner On Oct 23, 2013, at 8:39 AM, Ian Lepore wrote: > While creating drivers that work for a variety of SoCs, I increasingly > find myself coding sequences such as: >=20 > if (ofw_bus_is_compatible(dev, "fsl,imx51-fec")) > sc->fectype =3D FECTYPE_IMX51; > else if (ofw_bus_is_compatible(dev, "fsl,imx53-fec")) > sc->fectype =3D FECTYPE_IMX53; > else if (ofw_bus_is_compatible(dev, "fsl,imx6q-fec")) > sc->fectype =3D FECTYPE_IMX6; > else > sc->fectype =3D FECTYPE_GENERIC; >=20 > That's a short list as an example, eventually that driver may support = a > dozen SoCs. I'd like to add a helper routine that turns this into a > table lookup, patch attached. Any objections? It turns sequences = such > as the above into: >=20 > static struct ofw_compat_data compat_data[] =3D { > {"fsl,imx51-fec", FECTYPE_IMX51}, > {"fsl,imx53-fec", FECTYPE_IMX53}, > {"fsl,imx6q-fec", FECTYPE_IMX6}, > {NULL, FECTYPE_NONE}, > }; > /* ... */ > sc->fectype =3D ofw_bus_search_compatible(dev, compat_data)->ocd_data; >=20 > The search routine by design can't return NULL unless you pass it a = NULL > table pointer. That lets you provide whatever "not found" value in = the > table-end sentry that works best for the way your code is structured. >=20 > -- Ian >=20 > Index: sys/dev/ofw/ofw_bus_subr.h > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/dev/ofw/ofw_bus_subr.h (revision 256962) > +++ sys/dev/ofw/ofw_bus_subr.h (working copy) > @@ -47,6 +47,11 @@ struct ofw_bus_iinfo { > pcell_t opi_addrc; > }; >=20 > +struct ofw_compat_data { > + const char *ocd_str; > + uintptr_t ocd_data; > +}; > + > /* Generic implementation of ofw_bus_if.m methods and helper routines = */ > int ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *, phandle_t); > void ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *); > @@ -74,6 +79,10 @@ void ofw_bus_find_iparent(phandle_t); > int ofw_bus_is_compatible(device_t, const char *); > int ofw_bus_is_compatible_strict(device_t, const char *); >=20 > +/* Helper routine to search a list of compat props. */ > +const struct ofw_compat_data * > + ofw_bus_search_compatible(device_t, const struct ofw_compat_data = *); > + > /* Helper routine for checking existence of a prop */ > int ofw_bus_has_prop(device_t, const char *); > Index: sys/dev/ofw/ofw_bus_subr.c > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/dev/ofw/ofw_bus_subr.c (revision 256962) > +++ sys/dev/ofw/ofw_bus_subr.c (working copy) > @@ -197,6 +197,21 @@ ofw_bus_is_compatible_strict(device_t dev, const = c > return (0); > } >=20 > +const struct ofw_compat_data * > +ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data = *compat) > +{ > + > + if (compat =3D=3D NULL) > + return NULL; > + > + for (; compat->ocd_str !=3D NULL; ++compat) { > + if (ofw_bus_is_compatible(dev, compat->ocd_str)) > + break; > + } > + > + return (compat); > +} > + > int > ofw_bus_has_prop(device_t dev, const char *propname) > { >=20 > _______________________________________________ > freebsd-embedded@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-embedded > To unsubscribe, send any mail to = "freebsd-embedded-unsubscribe@freebsd.org"