From patchwork Mon Jan 27 15:49:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1229914 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=hjR1XRax; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 485vQq4XyVz9sPJ for ; Tue, 28 Jan 2020 02:52:51 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A133D81A44; Mon, 27 Jan 2020 16:51:26 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="hjR1XRax"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AF91981A32; Mon, 27 Jan 2020 16:50:31 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x143.google.com (mail-il1-x143.google.com [IPv6:2607:f8b0:4864:20::143]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9CBD681A23 for ; Mon, 27 Jan 2020 16:50:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x143.google.com with SMTP id b15so7845194iln.3 for ; Mon, 27 Jan 2020 07:50:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fcox4/rXqttmW3X/fAycOWkSJwm+H3N2nfNSHRN3n3o=; b=hjR1XRaxVsmj07ik1PnZGRp/dPKVnsejQLStqBAe+SOq8B29yaTGWhCJvrkzTYqil5 4DNbFHcsjZmTygHB/drBLE4HxmiIcF/gAR9fba1YOKDnmHnVWyVJCS4U9XvhYdkzzfsY fzehCUK/ep/0U1mvLuc/dsBmjS6CP7JahIDBY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fcox4/rXqttmW3X/fAycOWkSJwm+H3N2nfNSHRN3n3o=; b=mwGqctjLs4vpB4gl9aYNYoJrV9I9VlV6/8QX24AmSMNI6kdbTneBV8VK4HaYb4W5nQ 2BuTwt40vvz8JKfiEV6OppRl+XTTKxgu7OM5PHGSQJKRnFadl5/AeTCTkApd27DXRmSa R2OSpgxvrxR8djFelLo3Uo3wF5U5xuiY2nUJ6rT6NGaxDbjaFM9Kfmu7hbDBsPMXglCj KPKOHGXuKtYRuvWcIG+158fsfuYPNaq8Hs/bhXjKlmu4vwteH2O5o3aWn8blq6VLHJ6x BP8jDFIB42h1ysFVlBOklq2OFWXNDEF6H6QFbFF49Evc16+3v2kuPSix9N3YyxH12LPK 4EEw== X-Gm-Message-State: APjAAAVYbSPMaYK473gAOQJkO0IkWAzUiFCPC9Nd0sOc5UgIyaHW1xUm PJUrftS7xn3SvQBCo6rtvEvjgmWkXjM= X-Google-Smtp-Source: APXvYqxxywHSFmHmoCQBoeeZbAlHBipiTS27zrdgtVBuJr6Or9efuxbqoqIAAeyXqW/zya0RkTyjTQ== X-Received: by 2002:a92:5d8d:: with SMTP id e13mr15072122ilg.285.1580140219154; Mon, 27 Jan 2020 07:50:19 -0800 (PST) Received: from kiwi.bld.corp.google.com ([2620:15c:183:0:8223:87c:a681:66aa]) by smtp.gmail.com with ESMTPSA id v7sm3576560iom.58.2020.01.27.07.50.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2020 07:50:18 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Subject: [PATCH 13/21] dm: core: Add a way to iterate through children, probing each Date: Mon, 27 Jan 2020 08:49:48 -0700 Message-Id: <20200127084920.13.I307389a2de896b0eee9728fa4cb109f1b5379693@changeid> X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog In-Reply-To: <20200127154956.64368-1-sjg@chromium.org> References: <20200127154956.64368-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.26 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pavel Herrmann Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.101.4 at phobos.denx.de X-Virus-Status: Clean It is sometimes useful to process all children, making sure they are probed first. Add functions to help with this and a macro to make it more convenient. Signed-off-by: Simon Glass Signed-off-by: Simon Glass --- drivers/core/device.c | 22 ++++++++++++++++++++++ include/dm/device.h | 39 +++++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 19 +++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/drivers/core/device.c b/drivers/core/device.c index c948d8dbbc..89ea820d48 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -792,6 +792,28 @@ int device_find_child_by_name(const struct udevice *parent, const char *name, return -ENODEV; } +int device_first_child_err(struct udevice *parent, struct udevice **devp) +{ + struct udevice *dev; + + device_find_first_child(parent, &dev); + if (!dev) + return -ENODEV; + + return device_get_device_tail(dev, 0, devp); +} + +int device_next_child_err(struct udevice **devp) +{ + struct udevice *dev = *devp; + + device_find_next_child(&dev); + if (!dev) + return -ENODEV; + + return device_get_device_tail(dev, 0, devp); +} + int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp) { struct udevice *dev; diff --git a/include/dm/device.h b/include/dm/device.h index 2618952336..517ae7fc90 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -602,6 +602,28 @@ int device_first_child_ofdata_err(struct udevice *parent, */ int device_next_child_ofdata_err(struct udevice **devp); +/** + * device_first_child_err() - Get the first child of a device + * + * The device returned is probed if necessary, and ready for use + * + * @parent: Parent device to search + * @devp: Returns device found, if any + * @return 0 if found, -ENODEV if not, -ve error if device failed to probe + */ +int device_first_child_err(struct udevice *parent, struct udevice **devp); + +/** + * device_next_child_err() - Get the next child of a parent device + * + * The device returned is probed if necessary, and ready for use + * + * @devp: On entry, pointer to device to lookup. On exit, returns pointer + * to the next sibling if no error occurred + * @return 0 if found, -ENODEV if not, -ve error if device failed to probe + */ +int device_next_child_err(struct udevice **devp); + /** * device_has_children() - check if a device has any children * @@ -748,6 +770,23 @@ static inline bool device_is_on_pci_bus(const struct udevice *dev) for (int _ret = device_first_child_ofdata_err(parent, &dev); !_ret; \ _ret = device_next_child_ofdata_err(&dev)) +/** + * device_foreach_child_probe() - iterate through children, probing them + * + * This creates a for() loop which works through the available children of + * a device in order from start to end. Devices are probed if necessary, + * and ready for use. + * + * This stops when it gets an error, with @pos set to the device that failed to + * probe + * + * @pos: struct udevice * for the current device + * @parent: parent device to scan + */ +#define device_foreach_child_probe(pos, parent) \ + for (int _ret = device_first_child_err(parent, &dev); !_ret; \ + _ret = device_next_child_err(&dev)) + /** * dm_scan_fdt_dev() - Bind child device in a the device tree * diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index 8fe4425b21..cd65e42a88 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -891,3 +891,22 @@ static int dm_test_child_ofdata(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_child_ofdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test device_first_child_err(), etc. */ +static int dm_test_first_child_probe(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + int count; + + ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); + count = 0; + device_foreach_child_probe(dev, bus) { + ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); + ut_assert(dev->flags & DM_FLAG_ACTIVATED); + count++; + } + ut_asserteq(3, count); + + return 0; +} +DM_TEST(dm_test_first_child_probe, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);