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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 961B1C433FE for ; Sun, 30 Oct 2022 01:51:24 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 549798517B; Sun, 30 Oct 2022 02:48:16 +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=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="hn/9t+0P"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 330F78512B; Sun, 30 Oct 2022 02:47:54 +0100 (CET) Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) (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 AEEF585128 for ; Sun, 30 Oct 2022 02:47:50 +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-io1-xd2e.google.com with SMTP id y6so4475088iof.9 for ; Sat, 29 Oct 2022 18:47:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oKDzwdjthXnT9JR9LWhJLXEoxlGfFxTwGwtOMzcH16s=; b=hn/9t+0PtkmawcG657Mr8jI/zyYZdX82FTtLT/FDxPHhNDGAlYflEX5oMysnU9+jtC Rhhrjb+F8JYMRRbC/P/k+EVUyK4zdEquISyYQVmvyssGHkh+oeflYfYsrTDpBqRT4/IG MYwvqs28iy5cr/XaDGPH7C+LBY6JrjdjZFEqw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oKDzwdjthXnT9JR9LWhJLXEoxlGfFxTwGwtOMzcH16s=; b=clKgYxO4v4SFjU6zVh/bx+PJ3Dii+mxs6POJYUEgEf3W5qQnYkMGtKiobDMIOKiaxc 3eZGSB7TlixdHkgSdtjA3Njj5JBc/4e9wHJwUAnjyleX0Z9mwp4EeztDiiT4eQ7snTw0 w6N0FGosIQ8S8SxgxbG0XLMPb9jz/WgK6uLQuuglcrjlJDSylGNirAUvWBLxuCLki5x0 t78yVAlTjDBjYcVSc6L155PdIU9yngmR6GzPyKgbjccU0ss6V/kQTM+4rQYIlxZj5Yzk 1GM4hQGOJmd/iRRHhQ1HTqRVqIQw5HXxk7svoOYfPeI5Gm/FskfhI2UkY9JhlfHG/H+d bf+g== X-Gm-Message-State: ACrzQf31yNSF8x0cPG80z4VenU5tbrJWJ9XS5+SUZrZqQZAhv5ikI0VL R8z4DkG+V1igM52q7ZEE1RfZNyrtKfzGTg== X-Google-Smtp-Source: AMsMyM7d+yM06kimVcUXSsP0Tikjc7U8T0GAfwB76KfVdJunNeaULevoA/RMeURTIPnb4eBcexszJA== X-Received: by 2002:a05:6602:1554:b0:6bc:b47f:5f47 with SMTP id h20-20020a056602155400b006bcb47f5f47mr3268580iow.141.1667094469079; Sat, 29 Oct 2022 18:47:49 -0700 (PDT) Received: from sjg1.roam.corp.google.com (c-67-190-102-125.hsd1.co.comcast.net. [67.190.102.125]) by smtp.gmail.com with ESMTPSA id x66-20020a0294c8000000b00363c4307bb2sm810287jah.79.2022.10.29.18.47.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Oct 2022 18:47:48 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Heinrich Schuchardt , Simon Glass , AKASHI Takahiro , Marek Vasut , Mattijs Korpershoek , Michal Suchanek , Pavel Herrmann Subject: [PATCH 12/17] dm: blk: Tidy up obtaining a block device from its parent Date: Sat, 29 Oct 2022 19:47:14 -0600 Message-Id: <20221030014719.147327-13-sjg@chromium.org> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog In-Reply-To: <20221030014719.147327-1-sjg@chromium.org> References: <20221030014719.147327-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean This function now finds its block-device child by looking for a child device of the correct uclass (UCLASS_BLK). It cannot produce a device of any other type, so drop the superfluous check. Provide a version which does not probe the device, since that is often needed when setting up the device's platdata. Also fix up the function's comment. Signed-off-by: Simon Glass --- drivers/block/blk-uclass.c | 26 +++++++++++++++----------- include/blk.h | 28 +++++++++++++++++++++++++++- test/dm/blk.c | 2 +- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index bcc14a684be..66f9940b648 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -491,24 +491,28 @@ unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, return ops->erase(dev, start, blkcnt); } -int blk_get_from_parent(struct udevice *parent, struct udevice **devp) +int blk_find_from_parent(struct udevice *parent, struct udevice **devp) { struct udevice *dev; - enum uclass_id id; - int ret; - device_find_first_child(parent, &dev); - if (!dev) { + if (device_find_first_child_by_uclass(parent, UCLASS_BLK, &dev)) { debug("%s: No block device found for parent '%s'\n", __func__, parent->name); return -ENODEV; } - id = device_get_uclass_id(dev); - if (id != UCLASS_BLK) { - debug("%s: Incorrect uclass %s for block device '%s'\n", - __func__, uclass_get_name(id), dev->name); - return -ENOTBLK; - } + *devp = dev; + + return 0; +} + +int blk_get_from_parent(struct udevice *parent, struct udevice **devp) +{ + struct udevice *dev; + int ret; + + ret = blk_find_from_parent(parent, &dev); + if (ret) + return ret; ret = device_probe(dev); if (ret) return ret; diff --git a/include/blk.h b/include/blk.h index 58322c39eec..2f5d5121b14 100644 --- a/include/blk.h +++ b/include/blk.h @@ -416,10 +416,36 @@ int blk_next_free_devnum(enum uclass_id uclass_id); */ int blk_select_hwpart(struct udevice *dev, int hwpart); +/** + * blk_find_from_parent() - find a block device by looking up its parent + * + * All block devices have a parent 'media' device which provides the block + * driver for the block device, ensuring that access to the underlying medium + * is available. + * + * The block device is not activated by this function. See + * blk_get_from_parent() for that. + * + * @parent: Media device + * @devp: Returns the associated block device, if any + * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no + * UCLASS_BLK child + */ +int blk_find_from_parent(struct udevice *parent, struct udevice **devp); + /** * blk_get_from_parent() - obtain a block device by looking up its parent * - * All devices with + * All block devices have a parent 'media' device which provides the block + * driver for the block device, ensuring that access to the underlying medium + * is available. + * + * The block device is probed and ready for use. + * + * @parent: Media device + * @devp: Returns the associated block device, if any + * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no + * UCLASS_BLK child */ int blk_get_from_parent(struct udevice *parent, struct udevice **devp); diff --git a/test/dm/blk.c b/test/dm/blk.c index 35bd5318f0b..6f0cb98c861 100644 --- a/test/dm/blk.c +++ b/test/dm/blk.c @@ -160,7 +160,7 @@ static int dm_test_blk_get_from_parent(struct unit_test_state *uts) ut_assertok(blk_get_from_parent(dev, &blk)); ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev)); - ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk)); + ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk)); ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev)); ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk)); -- 2.38.1.273.g43a17bfeac-goog