Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6490839imu; Sun, 2 Dec 2018 19:47:02 -0800 (PST) X-Google-Smtp-Source: AFSGD/WKSMANqsAh74+QaK2A7/YqAHBmvl4sLu57oj4ocCfULj9LsYMZ3rIKZmGpc4dD6Lijwi7v X-Received: by 2002:a63:63c3:: with SMTP id x186mr12031390pgb.330.1543808822349; Sun, 02 Dec 2018 19:47:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543808822; cv=none; d=google.com; s=arc-20160816; b=yFgC/8zmK7liLCWP6ZDhSLJaoskT6G4mkRaa/PDj26z5xH3E/fOmLDccjViVM6IxwC CllYfiQAkwL5pdrmo4P5IKiCasmq2MwB7cpx6wVqAzv9moD16485hNcHfkXoySxsS+HX 3F9Bpy8MmH5WeCQjIDOrFBaAB7Yp64L9qDvxRKoRegbgYwper5lOtuhV8vYhZgN/w+m/ cvH55z6YzatFvCW6CSX7Y2lDXEDvikkjl/ue5YkALOLPYu9sfPvP6ZenmUTtuTyPjpkR h4GvL1ydiwpp0vcS0Xpp2oheA2sEj/O+9jEC+HjVMFGfl4gRG8rVm8Wlxabp7Ba2HwMB 8SUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=uJavE2jLlztF10FHutvbq9oubVzqjdImOIlTcb29Naw=; b=E8A55vS64hRqsAq9tZFtO6B5ShI+tyT+hbQafvw1vAO6/xZbJ8T2mmap7XbApkDE6W k7SV+NtnYJg427yHEWch3iWXppEnaqAVSf9mWueSmxGfWtOILVGk+XmoE3l2hhWTAl77 Rfig+leJAowPXaThS5rnNonm6ljJoIkANqIKJIkRKMI48LhoEVVx1/5ux6ehCe3jTP7P j6wpq7nBsJQrHj2Wo8jrA8bZJN2EmLBCMkslLdEBJmmShLXm6AD4s5bVVyNmlwE67N6G XHK9+KR5Ta5VmgvmjP9nyTu8vbmCHPnYrhvy3UjfWZ7UMaUtbpYXO9ptO+qf91AEkprU nnKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p126si11063172pgp.529.2018.12.02.19.46.47; Sun, 02 Dec 2018 19:47:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726024AbeLCDps (ORCPT + 99 others); Sun, 2 Dec 2018 22:45:48 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:15190 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725789AbeLCDps (ORCPT ); Sun, 2 Dec 2018 22:45:48 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 6C8A9418649A5; Mon, 3 Dec 2018 11:45:42 +0800 (CST) Received: from vm100-107-113-134.huawei.com (100.107.113.134) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.408.0; Mon, 3 Dec 2018 11:45:33 +0800 From: Yu Chen To: , , CC: , , Yu Chen , Greg Kroah-Hartman , Heikki Krogerus , Hans de Goede , Andy Shevchenko , "John Stultz" Subject: [PATCH v1 07/12] usb: roles: Find the usb role switch by also matching against the device node Date: Mon, 3 Dec 2018 11:45:10 +0800 Message-ID: <20181203034515.91412-8-chenyu56@huawei.com> X-Mailer: git-send-email 2.15.0-rc2 In-Reply-To: <20181203034515.91412-1-chenyu56@huawei.com> References: <20181203034515.91412-1-chenyu56@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [100.107.113.134] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds code for supporting find usb role switch by matching against the device node described using of_graph. Cc: Greg Kroah-Hartman Cc: Heikki Krogerus Cc: Hans de Goede Cc: Andy Shevchenko Cc: John Stultz Signed-off-by: Yu Chen --- drivers/usb/common/roles.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/drivers/usb/common/roles.c b/drivers/usb/common/roles.c index 99116af07f1d..0f48090c5c30 100644 --- a/drivers/usb/common/roles.c +++ b/drivers/usb/common/roles.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include static struct class *role_class; @@ -100,6 +102,38 @@ static void *usb_role_switch_match(struct device_connection *con, int ep, return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); } +static int __switch_match_by_of_node(struct device *dev, const void *name) +{ + if (!dev->parent || !dev->parent->of_node) + return 0; + + return of_node_name_eq(dev->parent->of_node, (const char *)name); +} + +static void *of_graph_find_match_by_type(struct device *dev, const char *ep_type) +{ + struct device_node *ep; + struct device_node *remote_parent; + struct device *role_switch; + + for_each_endpoint_of_node(dev_of_node(dev), ep) { + if (!ep->type || strcmp(ep->type, ep_type)) + continue; + + remote_parent = of_graph_get_remote_port_parent(ep); + if (!remote_parent || !remote_parent->name) + continue; + + role_switch = class_find_device(role_class, NULL, + remote_parent->name, __switch_match_by_of_node); + + return role_switch ? to_role_switch(role_switch) : + ERR_PTR(-EPROBE_DEFER); + } + + return NULL; +} + /** * usb_role_switch_get - Find USB role switch linked with the caller * @dev: The caller device @@ -114,6 +148,12 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev) sw = device_connection_find_match(dev, "usb-role-switch", NULL, usb_role_switch_match); + if (!IS_ERR_OR_NULL(sw)) { + WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); + return sw; + } + + sw = of_graph_find_match_by_type(dev, "usb-role-switch"); if (!IS_ERR_OR_NULL(sw)) WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); -- 2.15.0-rc2