Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp3002993lqt; Tue, 23 Apr 2024 07:57:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUgIU5VpNuoVjP+D6diJOcosg/oX2xElZHQHjvY/w/xdpDeaF3669oMOlE1axJcafdu5aYywaHEkanve3kg7DKn86aonpOfBeHhFpf5CA== X-Google-Smtp-Source: AGHT+IF5n9O/rhSBn0BT1vBqFTOICuUce7p/UIAVGnCphAyYeooSAfWkqvbD5Xo1EDhN+HoRqE3e X-Received: by 2002:a2e:95d4:0:b0:2d9:f818:1748 with SMTP id y20-20020a2e95d4000000b002d9f8181748mr10201526ljh.42.1713884263458; Tue, 23 Apr 2024 07:57:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713884263; cv=pass; d=google.com; s=arc-20160816; b=rF2kJoR/vBS4SPLEzBDywDtSqK9rXdDxwe+ZyMgHDozG1PkBY22AWG8iJN9PeltcKG FbqQD2msqiVopkeKR0po8w77YhQc0z2s5NXwtmxtqXLZeIBkcn19ivOJiJDoje3XvXjq QydkDeP+QNNBGMe/h4jC6FVBC0wmcF2KD1GRRG/+lCc/BiRO/0kjdqbBzQuAphYMHz8O aoSc8UzHii8VgVHv5qhZ9g9iQHETEva2xjdBrRxiHn1YGJvi49XpEZqHGC6chQulnelK t5/ErziAz3M9o7synstY5PdqIY6nUz6EiUMxoY6syIHy6JM3p/GQ5wAHEqJi2pUIbFhe y8tQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=hGsiCOFN4nE0bxasvx4D4akJDLOLdXoX82elYUQl1HU=; fh=jtCZ+nyqQSERrmggaaQZ5e/wVxSomMgCQ3miUDocXKU=; b=xj1f/Sn2zM2vEIEdIA+Ln8kzGQ8VF1SZEJJ8sKInkLkEm2Qb7MT5qIojZbrSFagdJG Szsv5pAyvfF2kKoOxUMhliEp+4uDRDFqGxQCnVx5VUZLj6TwbddHEPqKhLGYi0ssC3Jl SUI8pmUSvFrWBZGCWeDDrYWU8icffLlGpeKyAJLl6kx6MblCZmSQkjnVMDNXNH0iMptC 4N1ZCJvfh4q81dWxrV5vchUf7klqF/0U7AeDBzmmfVJacsMtH+rHi5vGf6CIylUHhyE6 Jxszum3fpKEfd7APnvIrt3CsGyp0gJVfdzUUQz2Y+XEhiFyPhqIYgz7rOFOz16sMPXUE Ys0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=ZaESqS0d; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-155403-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155403-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q3-20020aa7da83000000b0056e7896644asi7503165eds.638.2024.04.23.07.57.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 07:57:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-155403-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=ZaESqS0d; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-155403-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-155403-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2D71A1F22740 for ; Tue, 23 Apr 2024 14:57:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4AD1113B582; Tue, 23 Apr 2024 14:57:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="ZaESqS0d" Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B988B19BBA; Tue, 23 Apr 2024 14:57:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884240; cv=none; b=GsH/7LCMlAHavcSSe5ZzRDa/GEn4DTrCW6nopuH34dAHasASqxQOtr4MxCacfK586TsaaOcX+aM1XCC7Dir5L/22f6XzINZDFppn7NpTNLMqbUHh/bxbLtiU5icWfEIxWWo4V6hinzgl7SofvGpQdv7EXPmXI93T+UxGQ0SxO+k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884240; c=relaxed/simple; bh=9Z6TIVtIHsBGYS50SZqMPeE1FnH5U/FD9sZNT2L8PVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HmNYNhLFbEgEUI4kakpVqgnrvew173AUlI0CA8l8NSJLXpH7zIxafkd+iYS/ZheoQlV8cHzsNdypQ8+lyVqOjZg01j/4joeu8xO7hbLAZ9OmkUzmFqP5QNKaA45m2XadwAtMuKs6ciebT0aCOpJ2Xs5OPWMum4eDS88Qzl4XRes= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=ZaESqS0d; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id 09C4D2000D; Tue, 23 Apr 2024 14:57:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1713884236; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hGsiCOFN4nE0bxasvx4D4akJDLOLdXoX82elYUQl1HU=; b=ZaESqS0dsjp6EwjLf378LtSFa3TE4kfgV8Fs504anjVVXifQYL5rCQ1lvO9j+XILzYWjCM QNtOSZ+L3IHpKFgHLEyRBnkEAMk55ndleX816MIlg2km5g4yy84+jU2dVH1VONB+9zS0LL kmbwbh/EWE9xw6HiiJQk0LNAardpsDY/dl78Rnj4XzqHABw+wQ0/4zf5dnHw+9sPq3ouTj j893ZSegzz1hbFGiqGdnGjp3Jc14sCB4c6+0djNdkXy/1UA8iTZBp+MySYuSOGbzMi3OB8 h+XgOXxYAblRyVBvI+Zgcecsu/iZwK6loypiGbFYhlUd7QdqeT59lDUBOzsOhQ== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Bjorn Helgaas , Lizhi Hou , Rob Herring Cc: Max Zhen , Sonal Santan , Stefano Stabellini , Jonathan Cameron , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v4 1/2] driver core: Introduce device_{add,remove}_of_node() Date: Tue, 23 Apr 2024 16:56:58 +0200 Message-ID: <20240423145703.604489-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423145703.604489-1-herve.codina@bootlin.com> References: <20240423145703.604489-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: herve.codina@bootlin.com An of_node can be set to a device using device_set_node(). This function cannot prevent any of_node and/or fwnode overwrites. When adding an of_node on an already present device, the following operations need to be done: - Attach the of_node if no of_node were already attached - Attach the of_node as a fwnode if no fwnode were already attached This is the purpose of device_add_of_node(). device_remove_of_node() reverts the operations done by device_add_of_node(). Cc: stable@vger.kernel.org Signed-off-by: Herve Codina --- drivers/base/core.c | 52 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 54 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 521757408fc0..098ae335cb0a 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5127,6 +5127,58 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) } EXPORT_SYMBOL_GPL(set_secondary_fwnode); +/** + * device_remove_of_node - Remove an of_node from a device + * @dev: device whose device-tree node is being removed + */ +void device_remove_of_node(struct device *dev) +{ + dev = get_device(dev); + if (!dev) + return; + + if (!dev->of_node) + goto end; + + if (dev->fwnode == of_fwnode_handle(dev->of_node)) + dev->fwnode = NULL; + + of_node_put(dev->of_node); + dev->of_node = NULL; + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_remove_of_node); + +/** + * device_add_of_node - Add an of_node to an existing device + * @dev: device whose device-tree node is being added + * @of_node: of_node to add + */ +void device_add_of_node(struct device *dev, struct device_node *of_node) +{ + if (!of_node) + return; + + dev = get_device(dev); + if (!dev) + return; + + if (WARN(dev->of_node, "%s: Cannot replace node %pOF with %pOF\n", + dev_name(dev), dev->of_node, of_node)) + goto end; + + dev->of_node = of_node_get(of_node); + + if (!dev->fwnode) + dev->fwnode = of_fwnode_handle(of_node); + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_add_of_node); + /** * device_set_of_node_from_dev - reuse device-tree node of another device * @dev: device whose device-tree node is being set diff --git a/include/linux/device.h b/include/linux/device.h index 97c4b046c09d..1795121dee9a 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1127,6 +1127,8 @@ int device_offline(struct device *dev); int device_online(struct device *dev); void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); +void device_add_of_node(struct device *dev, struct device_node *of_node); +void device_remove_of_node(struct device *dev); void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); void device_set_node(struct device *dev, struct fwnode_handle *fwnode); -- 2.44.0