Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2189039pxm; Fri, 4 Mar 2022 11:01:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+2z2IeaY0kTVgLBMcDlbtPumAzYHKKdZY9EYUye1rIQCA8wrgyQTF3cYg/Ol9+mG/dwEz X-Received: by 2002:a17:903:1c5:b0:151:cd10:5a5 with SMTP id e5-20020a17090301c500b00151cd1005a5mr85763plh.26.1646420496352; Fri, 04 Mar 2022 11:01:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646420496; cv=none; d=google.com; s=arc-20160816; b=NcUn2IWte4NFwJmVop0aWrpQj9/f3yeJDT7OH9UWKbPTFfny2AJnDedMewHlOKC9bn /zRN2Lstmxr7/dsuroq/4VVmtgrLGhiT06pbwgRukryldaqvTkG38P853SIzGDrtYlY0 dl4QJraAl7tmyvox0hmTvWPPxBn74uWYYMLWHkxqbrkqsqmPpQImdKefpQQNKtwMXsRM 1qL6B9FW96Y20AoGQdEiMw0qyBfAHAdGddJ72BoVKAdPuRAg3nhLWIYGd2SRdfnocl8a RStvQ5a1vm+3DryQQHyh0zeT4Xkq1ZLzkvpcYZb9pjDkZzIN/7gsMGeXFjyh8F6RrXR+ ZzHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=M/9YlFAaOkbM16BQ1qAaFlrlzraJv98JxVR0CWuPkHU=; b=hTsPYci90oMqaiPX6W59+KERE1aT7O61W7S8NMVBcxHhmOGjo0Ma+1gJy8nNDRPPPU KcPCeN7OTfMlE0APO3bpF4I6O82iubGNeEL4uc0DhrWKH9mvo5D5MZmtl6e4XhywFrFd guAw/JCuk7F6AZnO/pSD1BYtG5/HUpPPRyTuWG0k0Tdrz39JynFxUTUv7pv3/iRMW3n8 EJegJIIV/Qp4SwpBLX3loISlZF175LTKG24DaFKFTRia4b8V0yZqP5fvSiiqvkjetigs e3Km5O71TG0wvEqrsb1CkvriV4Mb0sLAuuL03UPFrtJmHbCNq9jDexS1ev518KkxexKg kk7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fgbVb8Lr; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k4-20020aa788c4000000b004dff0e45b5fsi5883596pff.138.2022.03.04.11.01.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 11:01:36 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fgbVb8Lr; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D4B71DA010; Fri, 4 Mar 2022 10:59:58 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232005AbiCDNm2 (ORCPT + 99 others); Fri, 4 Mar 2022 08:42:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230061AbiCDNm1 (ORCPT ); Fri, 4 Mar 2022 08:42:27 -0500 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFA251B84F7; Fri, 4 Mar 2022 05:41:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646401298; x=1677937298; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=W8+cYexF94Vl6YzXjBdnxaTLtgugktdOPocSTG8FZjE=; b=fgbVb8Lri4D8f/UrJBnB5Xx45w7rPzcFwsUcbVxtsX7Z28UYVhwk1ksT 8ZSpIqcwfu4s0BdQZ/V8hr3EJa9zhOX2GTSsEXWV27AnH8mCYEkP3TYY5 lK41hTtGsP7I9DpWDC3DflYs/U68HkQEOKmauknplI9vnHjdB77zBNvdk v6BU59WYH6xVwcsQ55eDj/6LE72xBTSVLlwXO6Qd+i2t9bSGHdzxOkQG4 27U/IB8UIxIKgSq4v0N425err6QZhRQFV5k5cgepif0bI4yU9hJNVHvhc AiVqvzHCmg+V+5KXSzPc84GhP2teRBB8c/VBb2d1WL9MEM/R6mUAonnmL w==; X-IronPort-AV: E=McAfee;i="6200,9189,10275"; a="314691021" X-IronPort-AV: E=Sophos;i="5.90,155,1643702400"; d="scan'208";a="314691021" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 05:41:38 -0800 X-IronPort-AV: E=Sophos;i="5.90,155,1643702400"; d="scan'208";a="494331573" Received: from punajuuri.fi.intel.com (HELO paasikivi.fi.intel.com) ([10.237.72.43]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 05:41:35 -0800 Received: from paasikivi.fi.intel.com (localhost [127.0.0.1]) by paasikivi.fi.intel.com (Postfix) with ESMTP id 205D420087; Fri, 4 Mar 2022 15:41:33 +0200 (EET) Date: Fri, 4 Mar 2022 15:41:33 +0200 From: Sakari Ailus To: Andy Shevchenko Cc: Bjorn Andersson , Rob Herring , Daniel Scally , Heikki Krogerus , "Rafael J. Wysocki" , Hans de Goede , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, Dmitry Baryshkov Subject: Re: [PATCH v3 1/6] device property: Helper to match multiple connections Message-ID: References: <20220303223351.141238-1-bjorn.andersson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Andy, Bj?rn, On Fri, Mar 04, 2022 at 02:54:21PM +0200, Andy Shevchenko wrote: > On Thu, Mar 03, 2022 at 02:33:46PM -0800, Bjorn Andersson wrote: > > In some cases multiple connections with the same connection id > > needs to be resolved from a fwnode graph. > > > > One such example is when separate hardware is used for performing muxing > > and/or orientation switching of the SuperSpeed and SBU lines in a USB > > Type-C connector. In this case the connector needs to belong to a graph > > with multiple matching remote endpoints, and the Type-C controller needs > > to be able to resolve them both. > > > > Add a new API that allows this kind of lookup. > > ... > > > +static unsigned int fwnode_graph_devcon_matches(struct fwnode_handle *fwnode, > > + const char *con_id, void *data, > > + devcon_match_fn_t match, > > + void **matches, > > + unsigned int matches_len) > > +{ > > + struct fwnode_handle *node; > > + struct fwnode_handle *ep; > > + unsigned int count = 0; > > + void *ret; > > + > > + fwnode_graph_for_each_endpoint(fwnode, ep) { > > > + if (count >= matches_len && matches) { > > + fwnode_handle_put(ep); > > + return count; > > + } > > Wouldn't be the same as > > if (count >= matches_len) { > fwnode_handle_put(ep); > break; > } Don't you need to check for non-NULL matches here? I find return above easier to read. > > ? > > > + node = fwnode_graph_get_remote_port_parent(ep); > > + if (!fwnode_device_is_available(node)) { > > + fwnode_handle_put(node); > > + continue; > > + } > > + > > + ret = match(node, con_id, data); > > + fwnode_handle_put(node); > > + if (ret) { > > + if (matches) > > + matches[count] = ret; > > + count++; > > + } > > + } > > + return count; > > +} > > ... > > > +static unsigned int fwnode_devcon_matches(struct fwnode_handle *fwnode, > > + const char *con_id, void *data, > > + devcon_match_fn_t match, > > + void **matches, > > + unsigned int matches_len) > > +{ > > + struct fwnode_handle *node; > > + unsigned int count = 0; > > + unsigned int i; > > + void *ret; > > + > > + for (i = 0; ; i++) { > > > + if (count >= matches_len && matches) > > + return count; > > Ditto. > > > + node = fwnode_find_reference(fwnode, con_id, i); > > + if (IS_ERR(node)) > > + break; > > + > > + ret = match(node, NULL, data); > > + fwnode_handle_put(node); > > + if (ret) { > > + if (matches) > > + matches[count] = ret; > > + count++; > > + } > > + } > > + > > + return count; > > +} > > ... > > > +int fwnode_connection_find_matches(struct fwnode_handle *fwnode, > > + const char *con_id, void *data, > > + devcon_match_fn_t match, > > + void **matches, unsigned int matches_len) > > +{ > > + unsigned int count_graph; > > + unsigned int count_ref; > > + > > + if (!fwnode || !match) > > + return -EINVAL; > > > + count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match, > > + matches, matches_len); > > > + matches += count_graph; > > + matches_len -= count_graph; > > No, won't work when matches == NULL. > > Also, matches_len is expected to be 0 in that case (or at least being ignored, > check with vsnprintf() behaviour in similar case). > > So, something like this, perhaps > > if (matches && matches_len) { > matches += count_graph; > matches_len -= count_graph; > } Good find! > > > + count_ref = fwnode_devcon_matches(fwnode, con_id, data, match, > > + matches, matches_len); > > + > > + return count_graph + count_ref; > > +} > > -- Regards, Sakari Ailus