Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp1019846rwl; Fri, 4 Nov 2022 08:55:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7oNyQe5BvHb3Ywn+FIxnwucALq7rrFI9sCk+tFhZBPh5K7zvSOxRVXczH6f71WOR6etNLZ X-Received: by 2002:a17:906:ee81:b0:77e:829a:76e9 with SMTP id wt1-20020a170906ee8100b0077e829a76e9mr35965599ejb.207.1667577316408; Fri, 04 Nov 2022 08:55:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1667577316; cv=pass; d=google.com; s=arc-20160816; b=yKjI5ZA7oB8fsVDB6FJzMIm1QGGxydm0CK0HY+VCs2xHzHZ4VkoAWWXDVYjHQnYC73 kxmzNmRSeSofyj4SMjloe1tHCJ5CT9pJLwNGL4taBYTedrhTVF+S0WzcO4BEshxhA/n9 lLxIpOaIxr0bwW99NuUjt4owoyN+jTj79Po2I66M34phqt0somlFn3afVNykdci4TqvW Fb492Rzy+U+8d5XvFkRSllZ3INQyv+YEYlvwhRZFrEHN29uSlYJH5Np8qKb8duqqeA3R b7xIewLz9zyaYvNn7U/2bYpTh+2F6MfEHApRDLGZKrBd0KFSugtxE1FIYisq2C52m+Dv gUVw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jr1jJTtNe7KrtqSk+I5vkOJUn+sfVORhldP6YiP2ofU=; b=a62EPcvTTeVWd7/sOZRVEmzENUm88VtzchMlapBSepJZEfBLXWB64Y2JijhXuMNzWm WLfS7RI5BL+ZRM0kCXhgKQBNe4Iis0QI2GBOfCG3p9SdsLTYpD57Fv3NIbI/7WAaEObp pc/csDycmF6+tHJoUSJgV+u00yEs8XpserdwI61H7kFPHIh6H6B7ZVLaxuTdZFlhMNjj SrLuE1GIphUBiE3DQITuu3spgwCQC3ukMBeER4ECTNB3Pz7qQmN70NfpFclnYMSkiiVT n1JgbAKW3lTPyHff0ltbkL/zZ/VJN1ZLY+aH8BISb/jhbBQ03F6n9wyeiBjHMmyDZIlG Ev+Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=uQk0QI8c; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f11-20020a056402354b00b0045d8bff7b1asi6564177edd.403.2022.11.04.08.54.53; Fri, 04 Nov 2022 08:55:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=uQk0QI8c; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232634AbiKDPZM (ORCPT + 96 others); Fri, 4 Nov 2022 11:25:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232570AbiKDPYt (ORCPT ); Fri, 4 Nov 2022 11:24:49 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2084.outbound.protection.outlook.com [40.107.243.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B73AD2DA9C; Fri, 4 Nov 2022 08:24:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g2iRvlLp9aMZ3af75dNnOGHDhNCbLhBNTDe8ihnpQiBQyXzFw9ppp2CoY1+JiKVQUuqa6RFb/CW4PrZXefUPot8ene1R7YxLeV0BDlLJBgjMx0kwJ7PdSdYtmkhzHupP0R+ilHHurHcno3EUeo7S06d1+IcrU+ymHA1NvMtte0xiVk9iLh6IuqnRPVY0KKr7o+eiLfb0i72qTPcUyABzvU0e3j4tVd62U9yCOYI+7ivzYlgVlNgCtK5U59kZSBzQe0cPOZnRK0E3I4mL0d8AmQ1To2c32lbUS3X6uSpqmOWgJBRHwe9ujud/DMjKWkE/+fLeMl/EnovBgfiz2XA8yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jr1jJTtNe7KrtqSk+I5vkOJUn+sfVORhldP6YiP2ofU=; b=i5rL5YdmLbV084otdToENrDKmzrAoX0UREnU5+ackhZrVxbeAZQRDZalI81Jc7bTeV21duavNNb9Tub+4WOlTMyEAQpNz3TN9RSbhZNH1LPGlqBsMHJPjvpj7fV1223wvDJAl4k3CPcPYhqCV6KNDEXiCF46mVoxyq0CiWw2Off66FIvM9X24xTWNTnzKARWVncA9ST6dNGUci8jikXlQa4b2jHnR2mPgf/b4hIa3VSWoUKFr75ZAUkLPQceDV9v2dotzPcOsUrtXMptYNBmrc+DbwF2hPdLt5CiGqdzshgK0+uYOwqbkjaLrgEiJvJpmB7Q63XZNFRQEZOmOrgXSg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jr1jJTtNe7KrtqSk+I5vkOJUn+sfVORhldP6YiP2ofU=; b=uQk0QI8c7Kz2q5ramf8+1yElQiuBrNA6etbOpwU3A2LIC2P7My3IDUXkUcR/eeSrhIiHuGSKekgTpBTCG8ULh2qS2oEHnZVBqVcHxLzKVzOrZ8Axf1k6qEtSGC9HHFB/39UF6PjHLoBa+FlpwBncgAw15Gr+dkXamSnKltf3Uzc= Received: from MW4PR03CA0023.namprd03.prod.outlook.com (2603:10b6:303:8f::28) by DM4PR12MB5230.namprd12.prod.outlook.com (2603:10b6:5:399::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.22; Fri, 4 Nov 2022 15:24:21 +0000 Received: from CO1NAM11FT084.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8f:cafe::96) by MW4PR03CA0023.outlook.office365.com (2603:10b6:303:8f::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.22 via Frontend Transport; Fri, 4 Nov 2022 15:24:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1NAM11FT084.mail.protection.outlook.com (10.13.174.194) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5791.20 via Frontend Transport; Fri, 4 Nov 2022 15:24:20 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 4 Nov 2022 10:24:20 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 4 Nov 2022 10:24:19 -0500 Received: from xsjlizhih40.xilinx.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2375.31 via Frontend Transport; Fri, 4 Nov 2022 10:24:18 -0500 From: Lizhi Hou To: , , , , , CC: Lizhi Hou , , , , , , , Subject: [PATCH RFC V3 1/2] of: dynamic: Add interfaces for creating device node dynamically Date: Fri, 4 Nov 2022 08:24:13 -0700 Message-ID: <1667575454-18706-2-git-send-email-lizhi.hou@amd.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1667575454-18706-1-git-send-email-lizhi.hou@amd.com> References: <1667575454-18706-1-git-send-email-lizhi.hou@amd.com> MIME-Version: 1.0 Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT084:EE_|DM4PR12MB5230:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b96edaf-d855-4b0a-e55b-08dabe78a565 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LoHGLUucZc5Hnri7LuFCdbJ2bUU20T8Hsfid+h7uWg/Ilh+CW33d/rdOkEt388D9q317J63/Gz4VRZoYqXidnQ8emjlpBqTLjZdJRyiYHtmnAtiQz9yuf6DDcV89pMYFZYRDZ2/Vx8xFa+fJPRVXY1U3lI3u2Zg/HNpB4PczBL+BaD3zrxTY4LVQzo9AC70Vq7y2B5IfeBs+NBhgi2kkUdPJImgxNVvRAHAdbaJCg5Sx4UyHzMikkZeJWbXi4Snd+RkgQzrM35tvlf/sjcRDEnh7Pv/+kcHHmi+RUfBUNvF3GpHD3U2HygTvHKbLO3OiN/l87TSpv7fixfSh2BlnIBmiedfafcQ5KyXlCyvIVbaCE3zVMSy5f0QTD3ZFVxhhP+neJYDIL1C1hhYvRmq+KWIltqdbcaIE4EVsD62nlDZGdev9AIDWgJCGUIRyLP/J7zxJipJ7mUWr/ncBSurx24ywOTnyD3WEYmCI4qoZsYv9cJowqbx/dtB3r8kmBia+3Z8TX4h7Uz43b6pdkK8vWvywlEWh4/fWn4N7AalvjfYgyHNaTHoGhIt15agUsc0B3pbXusY7EtA1qyFVfsp+Vkqa5GQTLjuSfNDiVemjznqP4fb6Ojh+o0Br6K00dM9CrK9LCgyNcNjsZv1YqNVKw9GmThzHOdovAL1Y5tUyfVJ64bhRz3mQVs+j3+R27MVwsreK/CHtKgrEPhRutrQPKa6XXAPP0wXAlbN4PlucAMZX3GDtBOArvVaSLH2f8HDfbTBNw1TUQosbgbowRP3+DmEt/rnLrgOeXDLJZQ0GipGbhTzfRROkCoOVfmke547w X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(346002)(376002)(136003)(451199015)(40470700004)(36840700001)(46966006)(478600001)(2906002)(70586007)(70206006)(426003)(8676002)(83380400001)(47076005)(4326008)(36756003)(6666004)(40480700001)(36860700001)(316002)(54906003)(86362001)(110136005)(82310400005)(40460700003)(82740400003)(356005)(81166007)(26005)(41300700001)(2616005)(8936002)(5660300002)(186003)(336012)(44832011)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Nov 2022 15:24:20.5211 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3b96edaf-d855-4b0a-e55b-08dabe78a565 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT084.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5230 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org of_create_node() creates device node dynamically. The parent device node and full name are required for creating the node. It optionally creates an OF changeset and attaches the newly created node to the changeset. The device node pointer and the changeset pointer can be used to add properties to the device node and apply the node to the base tree. of_destroy_node() frees the device node created by of_create_node(). If an OF changeset was also created for this node, it will destroy the changeset before freeing the device node. Expand of_changeset APIs to handle specific types of properties. of_changeset_add_prop_string() of_changeset_add_prop_string_array() of_changeset_add_prop_u32_array() Signed-off-by: Lizhi Hou Signed-off-by: Sonal Santan Signed-off-by: Max Zhen Reviewed-by: Brian Xu --- drivers/of/dynamic.c | 187 +++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 24 ++++++ 2 files changed, 211 insertions(+) diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index cd3821a6444f..6b015b71e7fe 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -461,6 +461,72 @@ struct device_node *__of_node_dup(const struct device_node *np, return NULL; } +/** + * of_create_node - Dynamically create a device node + * + * @parent: Pointer to parent device node + * @full_name: Node full name + * @cset: Pointer to returning changeset + * + * Return: Pointer to the created device node or NULL in case of an error. + */ +struct device_node *of_create_node(struct device_node *parent, + const char *full_name, + struct of_changeset **cset) +{ + struct of_changeset *ocs; + struct device_node *np; + int ret; + + np = __of_node_dup(NULL, full_name); + if (!np) + return NULL; + np->parent = parent; + + if (!cset) + return np; + + ocs = kmalloc(sizeof(*cset), GFP_KERNEL); + if (!ocs) { + of_node_put(np); + return NULL; + } + + of_changeset_init(ocs); + ret = of_changeset_attach_node(ocs, np); + if (ret) { + of_changeset_destroy(ocs); + of_node_put(np); + kfree(ocs); + return NULL; + } + + np->data = ocs; + *cset = ocs; + + return np; +} +EXPORT_SYMBOL(of_create_node); + +/** + * of_destroy_node - Destroy a dynamically created device node + * + * @np: Pointer to dynamically created device node + * + */ +void of_destroy_node(struct device_node *np) +{ + struct of_changeset *ocs; + + if (np->data) { + ocs = (struct of_changeset *)np->data; + of_changeset_destroy(ocs); + kfree(ocs); + } + of_node_put(np); +} +EXPORT_SYMBOL(of_destroy_node); + static void __of_changeset_entry_destroy(struct of_changeset_entry *ce) { if (ce->action == OF_RECONFIG_ATTACH_NODE && @@ -934,3 +1000,124 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action, return 0; } EXPORT_SYMBOL_GPL(of_changeset_action); + +static int of_changeset_add_prop_helper(struct of_changeset *ocs, + struct device_node *np, + const struct property *pp) +{ + struct property *new_pp; + int ret; + + new_pp = __of_prop_dup(pp, GFP_KERNEL); + if (!new_pp) + return -ENOMEM; + + ret = of_changeset_add_property(ocs, np, new_pp); + if (ret) { + kfree(new_pp->name); + kfree(new_pp->value); + kfree(new_pp); + } + + return ret; +} + +/** + * of_changeset_add_prop_string - Add a string property to a changeset + * + * @ocs: changeset pointer + * @np: device node pointer + * @prop_name: name of the property to be added + * @str: pointer to null terminated string + * + * Create a string property and add it to a changeset. + * + * Return: 0 on success, a negative error value in case of an error. + */ +int of_changeset_add_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str) +{ + struct property prop; + + prop.name = (char *)prop_name; + prop.length = strlen(str) + 1; + prop.value = (void *)str; + + return of_changeset_add_prop_helper(ocs, np, &prop); +} +EXPORT_SYMBOL_GPL(of_changeset_add_prop_string); + +/** + * of_changeset_add_prop_string_array - Add a string list property to + * a changeset + * + * @ocs: changeset pointer + * @np: device node pointer + * @prop_name: name of the property to be added + * @str_array: pointer to an array of null terminated strings + * @sz: number of string array elements + * + * Create a string list property and add it to a changeset. + * + * Return: 0 on success, a negative error value in case of an error. + */ +int of_changeset_add_prop_string_array(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, + const char **str_array, size_t sz) +{ + struct property prop; + int i, ret; + char *vp; + + prop.name = (char *)prop_name; + + prop.length = 0; + for (i = 0; i < sz; i++) + prop.length += strlen(str_array[i]) + 1; + + prop.value = kmalloc(prop.length, GFP_KERNEL); + if (!prop.value) + return -ENOMEM; + + vp = prop.value; + for (i = 0; i < sz; i++) { + vp += snprintf(vp, (char *)prop.value + prop.length - vp, "%s", + str_array[i]) + 1; + } + ret = of_changeset_add_prop_helper(ocs, np, &prop); + kfree(prop.value); + + return ret; +} +EXPORT_SYMBOL_GPL(of_changeset_add_prop_string_array); + +/** + * of_changeset_add_prop_string - Add a property of 32 bit integers + * property to a changeset + * + * @ocs: changeset pointer + * @np: device node pointer + * @prop_name: name of the property to be added + * @u32: pointer to an array of 32 bit integers + * @sz: number of array elements + * + * Create a property of 32 bit integers and add it to a changeset. + * + * Return: 0 on success, a negative error value in case of an error. + */ +int of_changeset_add_prop_u32_array(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, + const u32 *array, size_t sz) +{ + struct property prop; + + prop.name = (char *)prop_name; + prop.length = sizeof(u32) * sz; + prop.value = (void *)array; + + return of_changeset_add_prop_helper(ocs, np, &prop); +} +EXPORT_SYMBOL_GPL(of_changeset_add_prop_u32_array); diff --git a/include/linux/of.h b/include/linux/of.h index 766d002bddb9..ba31036f0876 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1505,6 +1505,30 @@ static inline int of_changeset_update_property(struct of_changeset *ocs, { return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop); } + +struct device_node *of_create_node(struct device_node *parent, + const char *full_name, + struct of_changeset **cset); +void of_destroy_node(struct device_node *np); +int of_changeset_add_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str); +int of_changeset_add_prop_string_array(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, + const char **str_array, size_t sz); +int of_changeset_add_prop_u32_array(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, + const u32 *array, size_t sz); +static inline int of_changeset_add_prop_u32(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, + const u32 val) +{ + return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); +} + #else /* CONFIG_OF_DYNAMIC */ static inline int of_reconfig_notifier_register(struct notifier_block *nb) { -- 2.17.1