From mboxrd@z Thu Jan 1 00:00:00 1970 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 EE67FC433EF for ; Mon, 25 Apr 2022 05:32:54 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3174F83E85; Mon, 25 Apr 2022 07:32:38 +0200 (CEST) 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="U1E9l4TC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EEABB83DFE; Mon, 25 Apr 2022 07:32:20 +0200 (CEST) Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) (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 28E0F83E30 for ; Mon, 25 Apr 2022 07:32:13 +0200 (CEST) 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-ed1-x52d.google.com with SMTP id p4so1441882edx.0 for ; Sun, 24 Apr 2022 22:32:13 -0700 (PDT) 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=b55OzT9ylpkd7eCZKqy8jTiTk389x2leLW3Lve1okbA=; b=U1E9l4TCuNvheIGDDH8jXNV80xC24bBID/oknSO7xR3Df3cN5JitriIw8wCkhkanJh sQz6E3SLFblIpUn6AFDOyzw+9O1NWN2lZKy5pKN8ftaRm4gi5k7EhsPD/z7P8nzT1ZRa 0uN6czki7EfPDRFqkqQbD4aYbR+ta1TjsCVEM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b55OzT9ylpkd7eCZKqy8jTiTk389x2leLW3Lve1okbA=; b=7w2nnalrFnf3+L8ERozRtvjv/1VQQ3jWS0Yqo+WC5LZJa9HYiBY677V0nloyuo7CvH Dzig1Mrpnw/K1bJ07aJ0x64L2mzmQFNMjmJPChrJBdHufaynMk4JSzWwEl/+0Hpp5TIq 9XinbzMmT2q0n0PLQ8fYQhx3Y6pnbrJRgUBctIzhFBt8f2HSMjdl1Cu8NmoJkhr8x0uJ fFnlFbwet07WmGHyZ9hgPP7Ru+hASq42cYO8sbo2unfBeapHe8OqkZXv+MrbjzCQhroo vJMn43VEV57kP81VGq7d9rnat6XTw+2mG8YVStDlecXTv5mwhje0BaeZMOWeenzGHutr p4ZQ== X-Gm-Message-State: AOAM532pAk0k2Mld6kV3FNASGl2og+uezAhZ8VSjTkOklqVqYzJuZ8xC /rQu8Z8UrioREpliecbtRrN39Txu29B0XfF6 X-Google-Smtp-Source: ABdhPJzt/RnZbhkk9Ja4Y3k/G5KAlS1E74dWt3SHQGHoFTuDIK48tWyIFafCmGJpZ8x5wzyUhfOPag== X-Received: by 2002:a05:6402:3609:b0:425:a4bc:db86 with SMTP id el9-20020a056402360900b00425a4bcdb86mr17301879edb.98.1650864732419; Sun, 24 Apr 2022 22:32:12 -0700 (PDT) Received: from sjg1.c.hospitality.swisscom.com (d54c768e3.static.telenet.be. [84.199.104.227]) by smtp.gmail.com with ESMTPSA id s12-20020a1709062ecc00b006e8558c9a5csm3230784eji.94.2022.04.24.22.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Apr 2022 22:32:12 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Dennis Gilmore , Lukas Auer , Daniel Schwierzeck , Tom Rini , Michal Simek , Ilias Apalodimas , Steffen Jaeckel , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH v5 07/34] dm: core: Allow finding a uclass device by partial name Date: Sun, 24 Apr 2022 23:31:00 -0600 Message-Id: <20220425053127.19950-8-sjg@chromium.org> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog In-Reply-To: <20220425053127.19950-1-sjg@chromium.org> References: <20220425053127.19950-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.5 at phobos.denx.de X-Virus-Status: Clean In some cases two devices are related and the only way to tell is to check that the names partially patch. Add a way to check this without needing to create a new string for the comparison. Fix the comment for device_find_child_by_namelen() while we are here. Signed-off-by: Simon Glass --- (no changes since v4) Changes in v4: - Add a test also - Use new sphynx Return style Changes in v3: - Add new patch to support finding a uclass device by partial name drivers/core/uclass.c | 13 ++++++++++--- include/dm/device.h | 2 +- include/dm/uclass-internal.h | 16 ++++++++++++++++ test/dm/core.c | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 4b9b54f0f15..08d9ed82de2 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -274,8 +274,8 @@ int uclass_find_next_device(struct udevice **devp) return 0; } -int uclass_find_device_by_name(enum uclass_id id, const char *name, - struct udevice **devp) +int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len, + struct udevice **devp) { struct uclass *uc; struct udevice *dev; @@ -289,7 +289,8 @@ int uclass_find_device_by_name(enum uclass_id id, const char *name, return ret; uclass_foreach_dev(dev, uc) { - if (!strcmp(dev->name, name)) { + if (!strncmp(dev->name, name, len) && + strlen(dev->name) == len) { *devp = dev; return 0; } @@ -298,6 +299,12 @@ int uclass_find_device_by_name(enum uclass_id id, const char *name, return -ENODEV; } +int uclass_find_device_by_name(enum uclass_id id, const char *name, + struct udevice **devp) +{ + return uclass_find_device_by_namelen(id, name, strlen(name), devp); +} + int uclass_find_next_free_seq(struct uclass *uc) { struct udevice *dev; diff --git a/include/dm/device.h b/include/dm/device.h index e0f86f5df9f..b474888d025 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -799,7 +799,7 @@ int device_find_first_child_by_uclass(const struct udevice *parent, struct udevice **devp); /** - * device_find_child_by_name() - Find a child by device name + * device_find_child_by_namelen() - Find a child by device name * * @parent: Parent device to search * @name: Name to look for diff --git a/include/dm/uclass-internal.h b/include/dm/uclass-internal.h index daf856c03cf..3ddcdd21439 100644 --- a/include/dm/uclass-internal.h +++ b/include/dm/uclass-internal.h @@ -154,6 +154,22 @@ int uclass_find_first_device(enum uclass_id id, struct udevice **devp); */ int uclass_find_next_device(struct udevice **devp); +/** + * uclass_find_device_by_namelen() - Find uclass device based on ID and name + * + * This searches for a device with the exactly given name. + * + * The device is NOT probed, it is merely returned. + * + * @id: ID to look up + * @name: name of a device to find + * @len: Length of @name (the uclass driver name must have the same length) + * @devp: Returns pointer to device (the first one with the name) + * Return: 0 if OK, -ve on error + */ +int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len, + struct udevice **devp); + /** * uclass_find_device_by_name() - Find uclass device based on ID and name * diff --git a/test/dm/core.c b/test/dm/core.c index 2c73ecf54a0..ebd504427d1 100644 --- a/test/dm/core.c +++ b/test/dm/core.c @@ -1260,3 +1260,18 @@ static int dm_test_get_stats(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT); + +/* Test uclass_find_device_by_name() */ +static int dm_test_uclass_find_device(struct unit_test_state *uts) +{ + struct udevice *dev; + + ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev)); + ut_asserteq(-ENODEV, + uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev)); + ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5, + &dev)); + + return 0; +} +DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT); -- 2.36.0.rc2.479.g8af0fa9b8e-goog