From nobody Thu Oct 24 02:46:08 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XYqzj0XfHz5Zy1w; Thu, 24 Oct 2024 02:46:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XYqzh5bJHz46qN; Thu, 24 Oct 2024 02:46:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729737968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VwnRhVSosyjX+T5ezOsWhJkN3fgSmKM1+QTmPRS8KwM=; b=ka46aYji9nles4qaputEIQ9SRtdc4CmS8SxfXITs9+dGqga7SNwIAUq3nIugnkv0FYrWFd VAdyOSWOYsY2Fou0yd4yYByFuxq6tuOXfQjEDogQH7UZU1gqt3F0zIS3Af5RfgOPvEIrzk eEJ+AvNxEL26kvDP50uyO49J4mfi49jxuvIgz5giXwMnpmNtls8iz6mLVrkQPrnl5PMtXY ygho0q4bqhXwJy1v4wHk1n8SbU4I4KsIJbg3/Ln3rD9oOeYQSts3bswxLq6YQ0hNGeyz3E SWfNNORKnGSQZLYorDn5cWV2i7S8ZTSUOAaK0Ga4ArdNnamcgRBMt9KPeSLLpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729737968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VwnRhVSosyjX+T5ezOsWhJkN3fgSmKM1+QTmPRS8KwM=; b=ZN6m3AtC0UbA96oaoCWWd1hhh4xI95PgPWDgS8p0qe7B6fdngKnf8PhKBHqjUjKYLJA3+r VFrjgHKJiXXmzsWLlvhECAf2TYPXnUFeIbP/Fj3tk75cWYRP++u6f7dtd29Xt8dAMIEkU0 TOzoQ5TB4OctR7qvp+/sbIVIhi8ZcfVKdawV9zUz9/AqXAhJ1ezc68Kpi9QBN8SewCAe0K 2SW0yOMj/FiH/LhcCDsswBHqTJEJwA+NaCkQ8ZXTm934BLtsXvzMOuKyg5+Hrv2fhaqCYL dp4hT6WChyTzrpvIyAL/SD6bn05vCkDsRajaliDsXYCZyM0V1b7oEIR3hRzvpg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729737968; a=rsa-sha256; cv=none; b=hb2OokaJdUiVgL5p+TZ6toS465gAh/EMpQvEqNSaO+6SRvlHAjIaQLUngv1uSz4A+BlvzQ BlCHNxPzMIGvT+LgrNqTSkMAin910k/kv6/EMLJ1zfy77Ypv0lsRg79NMjObt2FMxppibk tc9AW4FsrWd2bRszFQR1EPwQREDN/KKa1OpA8xE2WqsFS3D5BZmFY0i1CRBbOVrgb+/V2b U1Y0YPjgamD1xFUYewh/YtI0CRW4sO/8ZGN4iQJDf0R3s1w3uQht3voafedWg27QoJKJyQ Gt9Neg1tclvrMdbDNTHp8X7IrS7EN98aU+AleO2aqGai8k9xWRd9Cb/dW0/Zuw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XYqzh5CLMzPSW; Thu, 24 Oct 2024 02:46:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49O2k8uH086165; Thu, 24 Oct 2024 02:46:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49O2k8vR086162; Thu, 24 Oct 2024 02:46:08 GMT (envelope-from git) Date: Thu, 24 Oct 2024 02:46:08 GMT Message-Id: <202410240246.49O2k8vR086162@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 2cda5031841f - stable/14 - device: add generic named per-device property List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 2cda5031841fa3745486a0f5c08981d266742569 Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=2cda5031841fa3745486a0f5c08981d266742569 commit 2cda5031841fa3745486a0f5c08981d266742569 Author: Konstantin Belousov AuthorDate: 2024-09-08 04:40:41 +0000 Commit: Konstantin Belousov CommitDate: 2024-10-24 02:44:40 +0000 device: add generic named per-device property (cherry picked from commit cb83af64f161b228171d6fb4f36d22493cefaa7f) --- sys/kern/subr_bus.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sys/sys/bus.h | 7 +++ 2 files changed, 127 insertions(+) diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 5965b8941b54..9e6973f6174c 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -105,6 +105,16 @@ struct devclass { struct sysctl_oid *sysctl_tree; }; +struct device_prop_elm { + const char *name; + void *val; + void *dtr_ctx; + device_prop_dtr_t dtr; + LIST_ENTRY(device_prop_elm) link; +}; + +static void device_destroy_props(device_t dev); + /** * @brief Implementation of _device. * @@ -141,6 +151,7 @@ struct _device { u_int order; /**< order from device_add_child_ordered() */ void *ivars; /**< instance variables */ void *softc; /**< current driver's variables */ + LIST_HEAD(, device_prop_elm) props; struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ @@ -1350,6 +1361,7 @@ make_device(device_t parent, const char *name, int unit) dev->flags |= DF_QUIET | DF_QUIET_CHILDREN; dev->ivars = NULL; dev->softc = NULL; + LIST_INIT(&dev->props); dev->state = DS_NOTPRESENT; @@ -1489,6 +1501,7 @@ device_delete_child(device_t dev, device_t child) return (error); } + device_destroy_props(dev); if (child->devclass) devclass_delete_device(child->devclass, child); if (child->parent) @@ -5990,6 +6003,113 @@ dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, return (strcmp(res->dln_path, cp) == 0); } +static struct device_prop_elm * +device_prop_find(device_t dev, const char *name) +{ + struct device_prop_elm *e; + + bus_topo_assert(); + + LIST_FOREACH(e, &dev->props, link) { + if (strcmp(name, e->name) == 0) + return (e); + } + return (NULL); +} + +int +device_set_prop(device_t dev, const char *name, void *val, + device_prop_dtr_t dtr, void *dtr_ctx) +{ + struct device_prop_elm *e, *e1; + + bus_topo_assert(); + + e = device_prop_find(dev, name); + if (e != NULL) + goto found; + + e1 = malloc(sizeof(*e), M_BUS, M_WAITOK); + e = device_prop_find(dev, name); + if (e != NULL) { + free(e1, M_BUS); + goto found; + } + + e1->name = name; + e1->val = val; + e1->dtr = dtr; + e1->dtr_ctx = dtr_ctx; + LIST_INSERT_HEAD(&dev->props, e1, link); + return (0); + +found: + LIST_REMOVE(e, link); + if (e->dtr != NULL) + e->dtr(dev, name, e->val, e->dtr_ctx); + e->val = val; + e->dtr = dtr; + e->dtr_ctx = dtr_ctx; + LIST_INSERT_HEAD(&dev->props, e, link); + return (EEXIST); +} + +int +device_get_prop(device_t dev, const char *name, void **valp) +{ + struct device_prop_elm *e; + + bus_topo_assert(); + + e = device_prop_find(dev, name); + if (e == NULL) + return (ENOENT); + *valp = e->val; + return (0); +} + +int +device_clear_prop(device_t dev, const char *name) +{ + struct device_prop_elm *e; + + bus_topo_assert(); + + e = device_prop_find(dev, name); + if (e == NULL) + return (ENOENT); + LIST_REMOVE(e, link); + if (e->dtr != NULL) + e->dtr(dev, e->name, e->val, e->dtr_ctx); + free(e, M_BUS); + return (0); +} + +static void +device_destroy_props(device_t dev) +{ + struct device_prop_elm *e; + + bus_topo_assert(); + + while ((e = LIST_FIRST(&dev->props)) != NULL) { + LIST_REMOVE_HEAD(&dev->props, link); + if (e->dtr != NULL) + e->dtr(dev, e->name, e->val, e->dtr_ctx); + free(e, M_BUS); + } +} + +void +device_clear_prop_alldev(const char *name) +{ + device_t dev; + + TAILQ_FOREACH(dev, &bus_data_devices, devlink) { + device_clear_prop(dev, name); + } +} + /* * APIs to manage deprecation and obsolescence. */ diff --git a/sys/sys/bus.h b/sys/sys/bus.h index 581e81352be4..eecff0c7c03e 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -889,6 +889,13 @@ device_location_cache_t *dev_wired_cache_init(void); void dev_wired_cache_fini(device_location_cache_t *dcp); bool dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, const char *at); +typedef void (*device_prop_dtr_t)(device_t dev, const char *name, void *val, + void *dtr_ctx); +int device_set_prop(device_t dev, const char *name, void *val, + device_prop_dtr_t dtr, void *dtr_ctx); +int device_get_prop(device_t dev, const char *name, void **valp); +int device_clear_prop(device_t dev, const char *name); +void device_clear_prop_alldev(const char *name); /** * Shorthand macros, taking resource argument