From patchwork Tue Jun 7 20:20:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12872640 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C343C43334 for ; Wed, 8 Jun 2022 00:42:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343495AbiFHAip (ORCPT ); Tue, 7 Jun 2022 20:38:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1840369AbiFHAFG (ORCPT ); Tue, 7 Jun 2022 20:05:06 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51A881D0BEA; Tue, 7 Jun 2022 13:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1654633261; x=1686169261; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=TOBWj/d3INrH0x6zLmSNwJlgmp7Kh2t/gQb1rcSIgAs=; b=R86wmGQA1M3jPdWDkfYGln+kT0wQufVxSzZ1R9k3lEhpKSEjLoXCNBnR FaYxzb8sGieVxkTtjq/HWCd9hiffCxOw6IjpQWKWZqzwtM6xOJeJ+NBSI NkW2VLP9Foy9GEtCCGyj1eyrd+/ajeJfjIctPBXdrZ1+lmPjFX+wcQrTo +h9sA25BAa52zmjCppP7NU8CED7oMtUi20ozg/58N7qUBNkQnTHOUeIih UQVeUdsV1Q93poQ+cJUr756u9g0kKoz62l36DhIlhOndKJAphMDUBsfVy patpfPx3BmfYDjZIUdOrb2RuiAsUftOxKXk0OcVsCIhnV5ozdwcX6RZyO w==; X-IronPort-AV: E=McAfee;i="6400,9594,10371"; a="257187474" X-IronPort-AV: E=Sophos;i="5.91,284,1647327600"; d="scan'208";a="257187474" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jun 2022 13:21:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,284,1647327600"; d="scan'208";a="709631225" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga004.jf.intel.com with ESMTP; 07 Jun 2022 13:20:58 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 1DD3F109; Tue, 7 Jun 2022 23:21:01 +0300 (EEST) From: Andy Shevchenko To: Greg Kroah-Hartman , Mark Brown , linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org Cc: "Rafael J. Wysocki" , Andy Shevchenko Subject: [PATCH v1 1/2] driver core: Introduce device_find_first_child() helper Date: Tue, 7 Jun 2022 23:20:57 +0300 Message-Id: <20220607202058.8304-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases. Signed-off-by: Andy Shevchenko --- drivers/base/core.c | 24 ++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 25 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 7cd789c4985d..972bfe975cd0 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3832,6 +3832,30 @@ struct device *device_find_child_by_name(struct device *parent, } EXPORT_SYMBOL_GPL(device_find_child_by_name); +/** + * device_find_first_child - device iterator for locating the fist child device. + * @parent: parent struct device + * + * This is similar to the device_find_child() function above, but it + * returns a reference to the first child device. + * + * NOTE: you will need to drop the reference with put_device() after use. + */ +struct device *device_find_first_child(struct device *parent) +{ + struct klist_iter i; + struct device *child; + + if (!parent) + return NULL; + + klist_iter_init(&parent->p->klist_children, &i); + child = get_device(next_device(&i)); + klist_iter_exit(&i); + return child; +} +EXPORT_SYMBOL_GPL(device_find_first_child); + int __init devices_init(void) { devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); diff --git a/include/linux/device.h b/include/linux/device.h index dc941997795c..20171a4358df 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -905,6 +905,7 @@ struct device *device_find_child(struct device *dev, void *data, int (*match)(struct device *dev, void *data)); struct device *device_find_child_by_name(struct device *parent, const char *name); +struct device *device_find_first_child(struct device *parent); int device_rename(struct device *dev, const char *new_name); int device_move(struct device *dev, struct device *new_parent, enum dpm_order dpm_order); From patchwork Tue Jun 7 20:20:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12872638 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 132A5CCA493 for ; Wed, 8 Jun 2022 00:38:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235594AbiFHAib (ORCPT ); Tue, 7 Jun 2022 20:38:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445247AbiFGXD0 (ORCPT ); Tue, 7 Jun 2022 19:03:26 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4856C3508A2; Tue, 7 Jun 2022 13:21:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1654633292; x=1686169292; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hj4Bi9Kc6WsQtRSs1xjE/8Rm+BZ4jVZvSsyYrBi4QgI=; b=LgipjXroiOlg7jBQvGbQTHB8KYEa3pA6NzA+PMNNBxQFiXafJFMi7zTA vg4Y5uLt4xfmnhPoGaA5Lf+pjnyU9kngDfZqtZDrznCisOwXDBNikOb6y be4+d7lClFJMC616wzpM5sCJaYyYOi0+bRR7LqOCOWcyHjxkZxH2jaF9J YpkhZIPKF6RvF5xGQz0ggpFbVHKUWYR1MDFo+5OIuSTSiVJQXg8xC2Bu7 A3gGqn0yG+yLkCkarbNbhPQ1k16PmaBKnDyIAoAR/5zR6RL4iv7Q1CnNi ph1S7wu2S2D4IVEfT49Q9rKKKm56FLyPexJOkhCe7W3nxXC3brwxQkNJQ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10371"; a="275518398" X-IronPort-AV: E=Sophos;i="5.91,284,1647327600"; d="scan'208";a="275518398" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jun 2022 13:21:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,284,1647327600"; d="scan'208";a="532772013" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 07 Jun 2022 13:20:58 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 2B63649; Tue, 7 Jun 2022 23:21:02 +0300 (EEST) From: Andy Shevchenko To: Greg Kroah-Hartman , Mark Brown , linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org Cc: "Rafael J. Wysocki" , Andy Shevchenko Subject: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach Date: Tue, 7 Jun 2022 23:20:58 +0300 Message-Id: <20220607202058.8304-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220607202058.8304-1-andriy.shevchenko@linux.intel.com> References: <20220607202058.8304-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org We have already a helper to get the first child device, use it and drop custom approach. Signed-off-by: Andy Shevchenko Reviewed-by: Rafael J. Wysocki --- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index ea09d1b42bf6..87dc8773108b 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) } EXPORT_SYMBOL_GPL(spi_slave_abort); -static int match_true(struct device *dev, void *data) -{ - return 1; -} - static ssize_t slave_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev); struct device *child; - child = device_find_child(&ctlr->dev, NULL, match_true); + child = device_find_first_child(&ctlr->dev); return sprintf(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL); } @@ -2644,7 +2639,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr, if (rc != 1 || !name[0]) return -EINVAL; - child = device_find_child(&ctlr->dev, NULL, match_true); + child = device_find_first_child(&ctlr->dev); if (child) { /* Remove registered slave */ device_unregister(child);