Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2823442lqp; Mon, 25 Mar 2024 10:09:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWNl04rNRV9Ln0o7lRhiD+XTxf7PYXqdau0rkXg5PiUYMFDwUGWTKt427Bmt4DiH7TzlEBBEK6ZRDZY1D1WpqX6DIYgyxESk+2qrnJRQ== X-Google-Smtp-Source: AGHT+IHku/Tghc2x2eWjCgaYMXdM1Lr63VDjcXr7zjGefCMiRcgRqzd3o9k8O6Q1JBkfvrFH2TTp X-Received: by 2002:a05:6102:1519:b0:476:e7af:ec3e with SMTP id f25-20020a056102151900b00476e7afec3emr4137539vsv.3.1711386578719; Mon, 25 Mar 2024 10:09:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711386578; cv=pass; d=google.com; s=arc-20160816; b=fMPLBlgK20pXKvK0Zo46S043HQfkecb+UgkDyS7/Vmo4Gvw7mUWLaY8FITXL+zlti6 5/9ipQqoNY9tKH0+mmNx+NOOyv25HflxDD/w/Mlww0PP6vNcj2TyXddjI5TrbXxhKdz4 PQEleEt2XQKtZ5+PmFt/a780Ax21pfjKTS/KsHeg3r0glbwLQtcM1UVx8IrczUxYAnXN q+dxe8t6YfhhReKb6xvwWzI6gQJQmY5uxxXtzrmADzcurVzdwh4wZhlKwGJssDmEU2I2 hnVhi6PQGHCc38Lc1uO8ovYepOEAkDbRtqcyaiBsU0nWMUyNIQ3Q5R1JRApqN8DG9cgD 9qcA== 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=HQTGLkwc5FM7PqkTkKIyxo75nWhT3EHFGFIKtu+4NBE=; fh=jtCZ+nyqQSERrmggaaQZ5e/wVxSomMgCQ3miUDocXKU=; b=pswBQL/n4Ss9iXScdJwK4E2Lr4ep6434TsGUX8YbFNCERCPeF5nMdnOiXdOP//q3bc Avs7gzVnllH30crAxEMvgpOFVUh6SrAxqXzZ7UuvR9U6sWACSZFBwG250+DQFcGbqzC2 aB3OWSyw1SKZ/qbvFbR3zHK2YDkRLWa+7U2ngdsgZJCnFlnw5AF7MsNny8Z6y4uTz9uP ngKew4BuUExRvc3joG4EUsFxHqQ/GcYzvDSAUMAgUJCRRWsAU+mGIzJ0bWpjxkImMDL/ bqG3fwrP3O3Hx3IrHmzW0rAbeyx6SJ85DKaeK7ZI1JJmCpQrTBnhJwR5TqDSIn8u3I0S F/RA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=kr5O9eyd; 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-117396-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117396-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id bm10-20020a056102510a00b0047823e329b6si297069vsb.38.2024.03.25.10.09.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 10:09:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117396-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=kr5O9eyd; 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-117396-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117396-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 617E01C2FD6A for ; Mon, 25 Mar 2024 17:09:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7773A535D4; Mon, 25 Mar 2024 15:39:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="kr5O9eyd" Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 E580713AA42; Mon, 25 Mar 2024 15:39:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711381175; cv=none; b=PCBbM+i1qzqpNW5pbXJw08xqMej1xx2JH7GInFCcNS91La/O0v4EB5C9csdkPBLGEx8rA8oX6oqk4i2VKWKMetr28viSJr8bWRv+gspQ3YgwBALs4IgNyDjIi4Yg7pX78KpTQmdjK9OndHtt/T8xBpNYJ5rdfD+EuTWeDzsQ/SI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711381175; c=relaxed/simple; bh=h/gSBHnRArX2VKnlimaWRqSx2BilNLtlrRscxKyKgKk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YtaLLf5DL893BqIuOEheMxAiBMbolKWiKgVY+90h+ZPhMHgTxJ2s3aQ629MoZt1a/6ZRTuayFaUpmFNn8fwNp6OXCcC1Z6WirCZoBX16C4veMewYTansdof9saLShwStZNyktzhdpkbcbrMHIuFWmh07lvotgN+Zd9/kfISOdVE= 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=kr5O9eyd; arc=none smtp.client-ip=217.70.183.201 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 0B9651BF209; Mon, 25 Mar 2024 15:39:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1711381169; 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=HQTGLkwc5FM7PqkTkKIyxo75nWhT3EHFGFIKtu+4NBE=; b=kr5O9eyd+6zYEO56uU+79EPm5uo3S0MZIDxkyj+fOYVQU4lPi2/ID9lCJ8QQFyakvPO0ID ucagZ3o+UBnKsxkeljBg4xvOWw/hpw8AL/k1QWXgcd6afKqX9dfPILD+qXpLlBcdfpF8ZM 7/vMR4MErbBW7kgCEPnx3qk9tSbRXxaCRJaENLv3fXZfkaqyruCgLZ18mB55GDVCb9tXA1 3RPk/1poj5uGYHHROgzoSpA3N5/20RzZzvaROBNKkSqnzJ/d3BIXjDpBFKGWDv7IAcPEet mUnwRENMB3oEft5vt1x5r6x/fAUo8UplhyqWhAm3Q4BzZ3vYiztN8XXADpcLPA== 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 v3 1/2] driver core: Introduce device_{add,remove}_of_node() Date: Mon, 25 Mar 2024 16:39:14 +0100 Message-ID: <20240325153919.199337-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240325153919.199337-1-herve.codina@bootlin.com> References: <20240325153919.199337-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 duplicated from an existing device using device_set_of_node_from_dev() or initialized using device_set_node() In both cases, these functions have to be called before the device_add() call in order to have the of_node link created in the device sysfs directory. Further more, these 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 - Create the of_node sysfs link if needed 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 | 74 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 76 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 521757408fc0..7e3af0ad770a 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5127,6 +5127,80 @@ 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; + + sysfs_remove_link(&dev->kobj, "of_node"); + + 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) +{ + int ret; + + if (!of_node) + return; + + dev = get_device(dev); + if (!dev) + return; + + if (dev->of_node) { + dev_warn(dev, "Replace node %pOF with %pOF\n", dev->of_node, of_node); + device_remove_of_node(dev); + } + + dev->of_node = of_node_get(of_node); + + if (!dev->fwnode) + dev->fwnode = of_fwnode_handle(of_node); + + if (!dev->p) { + /* + * device_add() was not previously called. + * The of_node link will be created when device_add() is called. + */ + goto end; + } + + /* + * device_add() was previously called and so the of_node link was not + * created by device_add_class_symlinks(). + * Create this link now. + */ + ret = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); + if (ret) + dev_warn(dev, "Error %d creating of_node link\n", ret); + +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