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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19B9BC433F5 for ; Sat, 23 Oct 2021 23:31:39 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 990D760C4D for ; Sat, 23 Oct 2021 23:31:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 990D760C4D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6287183580; Sun, 24 Oct 2021 01:30:45 +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="ge0/ECPr"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 824CA8357A; Sun, 24 Oct 2021 01:27:29 +0200 (CEST) Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) (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 3427183537 for ; Sun, 24 Oct 2021 01:27:01 +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-oi1-x235.google.com with SMTP id z126so9995637oiz.12 for ; Sat, 23 Oct 2021 16:27:01 -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=SaqBVlc56vEBeqGjN+xzo9BlVthLML6DRwwT5UtwNnk=; b=ge0/ECPrSPA1u+c9+wmhsnmfxBhyj3dza8srcxqOMRpyVpKCRko9BEwrhe+GcE/zGG bj5/1mBgW1pubcu4WNXSRf1piYDqYY+Zj5lPPG8e6hW+R7QgV0wu0XwmLcAhEbmEd7+3 KWRGbdGXjG/hzSJxfdrXcP7zpu87upqW9l3Tw= 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=SaqBVlc56vEBeqGjN+xzo9BlVthLML6DRwwT5UtwNnk=; b=Z+zPfONqcOUym4+ci9/yYqO7JyrXJQyEX1NvZ4/F+8gQfyirTV9s1Za8+tyFgiW+/o todf4BUOXWMnXowhxIk1tgD4OwBuLBTgBJZxdXP/RY2PV7pgcayMvRRYPp/csp2fAn0P C+hYGDP1HnQIBH846KItW5dcXdIS8zmAbxGFO5Az27rWW/jqIlp7QNV/fsNFN0xmxWpO fi9wJJpvPlow4v9dcpHAD8EVcB+hV60Ik+wIHdKn6873XRS2HjDzlIjUFI16L6+r1cvx nMLwToE2wx7uqiZI0/R+3v05bmC/Av1kzMz987g/Xx/dz8T/wfrxV8xLzgJhk++arvY+ 9fMg== X-Gm-Message-State: AOAM531j5UmnVKNqsF+Z+7zl5H0ysq97bh1X8AZZnznNOcRVJtPeSXnD 1x0Pzz0N//EIKphk2Hr6KoMfdFsp+CNs3g== X-Google-Smtp-Source: ABdhPJxoOSOPN6ysdh+gRrkmlKXP18SSrOfl7StBDoLSsFj62ojDTiibPECMxapUWB9ijW5k1zL6Zg== X-Received: by 2002:a54:4e8f:: with SMTP id c15mr3072557oiy.113.1635031619628; Sat, 23 Oct 2021 16:26:59 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id l24sm2253885oop.4.2021.10.23.16.26.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 16:26:59 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Michal Simek , Heinrich Schuchardt , Tom Rini , Ilias Apalodimas , Daniel Schwierzeck , Steffen Jaeckel , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Lukas Auer , Dennis Gilmore , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH v2 14/41] dm: core: Allow finding children / uclasses by partial name Date: Sat, 23 Oct 2021 17:26:08 -0600 Message-Id: <20211023232635.9195-13-sjg@chromium.org> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211023232635.9195-1-sjg@chromium.org> References: <20211023232635.9195-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean In some cases it is useful to search just by a partial name, such as when looking for a sibling device that has a common name substring. Add helper functions to handle these requirements. Signed-off-by: Simon Glass --- (no changes since v1) drivers/core/device.c | 13 ++++++++++--- drivers/core/uclass.c | 9 +++++++-- include/dm/device.h | 12 ++++++++++++ include/dm/uclass.h | 9 +++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/drivers/core/device.c b/drivers/core/device.c index d7a778a2413..77e30dcd0db 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -894,15 +894,16 @@ int device_find_first_child_by_uclass(const struct udevice *parent, return -ENODEV; } -int device_find_child_by_name(const struct udevice *parent, const char *name, - struct udevice **devp) +int device_find_child_by_namelen(const struct udevice *parent, const char *name, + int len, struct udevice **devp) { struct udevice *dev; *devp = NULL; list_for_each_entry(dev, &parent->child_head, sibling_node) { - if (!strcmp(dev->name, name)) { + if (!strncmp(dev->name, name, len) && + strlen(dev->name) == len) { *devp = dev; return 0; } @@ -911,6 +912,12 @@ int device_find_child_by_name(const struct udevice *parent, const char *name, return -ENODEV; } +int device_find_child_by_name(const struct udevice *parent, const char *name, + struct udevice **devp) +{ + return device_find_child_by_namelen(parent, name, strlen(name), devp); +} + int device_first_child_err(struct udevice *parent, struct udevice **devp) { struct udevice *dev; diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 2fede896bfb..3de5f27fe4a 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -180,20 +180,25 @@ void uclass_set_priv(struct uclass *uc, void *priv) uc->priv_ = priv; } -enum uclass_id uclass_get_by_name(const char *name) +enum uclass_id uclass_get_by_name_len(const char *name, int len) { int i; for (i = 0; i < UCLASS_COUNT; i++) { struct uclass_driver *uc_drv = lists_uclass_lookup(i); - if (uc_drv && !strcmp(uc_drv->name, name)) + if (uc_drv && !strncmp(uc_drv->name, name, len)) return i; } return UCLASS_INVALID; } +enum uclass_id uclass_get_by_name(const char *name) +{ + return uclass_get_by_name_len(name, strlen(name)); +} + int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp) { struct udevice *iter; diff --git a/include/dm/device.h b/include/dm/device.h index 3028d002ab0..daf28a0a457 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -758,6 +758,18 @@ int device_find_first_child_by_uclass(const struct udevice *parent, enum uclass_id uclass_id, struct udevice **devp); +/** + * device_find_child_by_name() - Find a child by device name + * + * @parent: Parent device to search + * @name: Name to look for + * @len: Length of the name + * @devp: Returns device found, if any + * @return 0 if found, else -ENODEV + */ +int device_find_child_by_namelen(const struct udevice *parent, const char *name, + int len, struct udevice **devp); + /** * device_find_child_by_name() - Find a child by device name * diff --git a/include/dm/uclass.h b/include/dm/uclass.h index 15e5f9ef5bc..aea2f34fce1 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -172,6 +172,15 @@ int uclass_get(enum uclass_id key, struct uclass **ucp); */ const char *uclass_get_name(enum uclass_id id); +/** + * uclass_get_by_name() - Look up a uclass by its driver name + * + * @name: Name to look up + * @len: Length of name + * @returns the associated uclass ID, or UCLASS_INVALID if not found + */ +enum uclass_id uclass_get_by_name_len(const char *name, int len); + /** * uclass_get_by_name() - Look up a uclass by its driver name * -- 2.33.0.1079.g6e70778dc9-goog