From patchwork Wed Oct 2 23:16:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 175043 Delivered-To: patches@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp1417324ill; Wed, 2 Oct 2019 16:16:24 -0700 (PDT) X-Received: by 2002:a17:902:7b84:: with SMTP id w4mr6451460pll.63.1570058184098; Wed, 02 Oct 2019 16:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570058184; cv=none; d=google.com; s=arc-20160816; b=AWLpU9tmUTrafsDgmOP0ReQyUpvT6OmQauXVTSobgeJ99ky6NXVJVtTdtKMFzyOTvp YavoNogiONpnAU52EXqzwUAxnSp3lWKcASMLepDrWgf0H6AyC+jyNG3Wxzfeu1NJfELE PQYWqnllElKvugK8+onSt66dtLcS+VSmBnNdC5pHh/4IP9FQoSUV6IxaEHzJdmp2AcF9 0tjjomeLj4CBfFnohOS2Qh2UsBlWPiPbyUeoHMHiypyUz8wmMLLibe3Ibfc0K6ApIqS+ FtqJ77vwajEh7c0yDyRUIo2atciG7oSKmY81EYij+W7fOKTTD5xwJLx1rMNmwxrUx5tt 2w8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rU3ZIe1wEyPAlyBuDaaRw79vpye8Qn/RpKU+olhtNWc=; b=MbGNLQ1qfz/Tmod7COf90MPRmjqvx0Wb+5gNQOBsgH3CuhVIwrFj5nvEEYjC5Ctnzm VWM1OVXhfWgCcKqrkT1vUOOPD4Y+kaKR0gsTKJYnL/lIK6zmlgZB1uQiTrZ1CM/csy3p 4pM6wTLpMxtQfwvReoR60csPFajA3QTwGLvIfbJBpatF4ly0JjQa6DPAvTODkJydyWyC IFeLvDMPYnuyEx8vOEjUQInxlR4dtdotIgDAMfD8fOWdGOOLtUttrxdQKJEINLFXvmAm m/wHiB7fjjqMxM+YnWPIYi/X0XB/KYNVtpCsJcLd8O6yvkBkOHugakBRWHSIFVP4kcoF rKSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yUio+wwG; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id 33sor797880pla.11.2019.10.02.16.16.24 for (Google Transport Security); Wed, 02 Oct 2019 16:16:24 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yUio+wwG; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rU3ZIe1wEyPAlyBuDaaRw79vpye8Qn/RpKU+olhtNWc=; b=yUio+wwG5BWebxxhvpw7haRUyEyivo6OPVbEi3XIn0sz8A+fsfgZW/IZSGk9ec0w4f k4ZlnzlypckTUxWu2hXxCWjxs8vw7UXlr95Mwpk0gklCavpWrE2yLme/muin40Hf1x9J ZtzTL/CsWMQoKLjUj4m/nIAlZgEjN2F9K2zALdSWKf4ca4eIshDHFjyScWISkS9ezCL6 xakFfbbDqt6ZTYKTEyt6iPYey0xWjdaWccEK8xBkX2tPDAq9m0/MdlVA+9kVeTC+Vabd 9hM4hLA32mrfL5Y1epXeavibzLHX300cNBtFli9v8J5PuuZWcPHBHLt/wi68gP7/DDEz DmXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rU3ZIe1wEyPAlyBuDaaRw79vpye8Qn/RpKU+olhtNWc=; b=bOu+/SM977wMd7WAHDe2WqjSEtXF+oTOcS/csA7y92I13jgYFrDq0A8CN6gdSN+Uic 7mThdNRjwUxaFQHuGdVO2b+xr9JpqEdBZsfld6HAYAK8mC3vLtb1CnG31wTujwp4f5s/ VLwVJeDMPagj/uzLK+EJM/mKWKRpN5vWrlDcHnbcuAWq1g5rgDoW6mi0l/rCrBtRrDfW kGClt6XII1Da+o6WsAbqEO6/ZND1aPe4K/SF+iMmVvgm+LFIQNRNOWMOTcgG5xfH8vli 4XkuTNUVVvFZ3ImwwX901L4OscyfAB3qW8GU0MMgH8yWLkgutGAPEMKyASHuC9xQqdIT GHXg== X-Gm-Message-State: APjAAAW+uOA3LQCB7EvbvWwZN0vZJ7SHKuLsg7xVHAEfxVDa6PUjk11f iJTSVliQQRi4Irqx6lZZBvPfvq91 X-Google-Smtp-Source: APXvYqwcoB4Ky8kF2Dkr6DmmT3lpgIWscDqSSRfTpsmolukj/DFBnWE3VjYEeGe1NHexI2kCdvgjsw== X-Received: by 2002:a17:902:ff08:: with SMTP id f8mr6340368plj.309.1570058183692; Wed, 02 Oct 2019 16:16:23 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:22 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Yu Chen , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC][PATCH 1/3] dt-bindings: usb: generic: Add role-switch-default-host binding Date: Wed, 2 Oct 2019 23:16:15 +0000 Message-Id: <20191002231617.3670-2-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> Add binding to configure the default role the controller assumes is host mode when the usb role is USB_ROLE_NONE. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Signed-off-by: John Stultz --- Documentation/devicetree/bindings/usb/generic.txt | 5 +++++ 1 file changed, 5 insertions(+) -- 2.17.1 diff --git a/Documentation/devicetree/bindings/usb/generic.txt b/Documentation/devicetree/bindings/usb/generic.txt index cf5a1ad456e6..013782fde293 100644 --- a/Documentation/devicetree/bindings/usb/generic.txt +++ b/Documentation/devicetree/bindings/usb/generic.txt @@ -34,6 +34,11 @@ Optional properties: the USB data role (USB host or USB device) for a given USB connector, such as Type-C, Type-B(micro). see connector/usb-connector.txt. + - role-switch-default-host: boolean, indicating if usb-role-switch is enabled + the device default operation mode of controller while + usb role is USB_ROLE_NONE is host mode. If this is not + set or false, it will be assumed the default is device + mode. This is an attribute to a USB controller such as: From patchwork Wed Oct 2 23:16:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 175044 Delivered-To: patches@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp1417353ill; Wed, 2 Oct 2019 16:16:26 -0700 (PDT) X-Received: by 2002:a62:ed19:: with SMTP id u25mr7668462pfh.40.1570058186586; Wed, 02 Oct 2019 16:16:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570058186; cv=none; d=google.com; s=arc-20160816; b=JNmuiu+XwuDwr9ZcodWZSH6z7Yfl6b5X7ouBsi91jinZat58MGl7fdSZUK+os4TMpI r9fGfRWz67kvNtqx2ZkDyHvPkumXXLOL8cOtjQNcvi7a1Tr3FWk75eIuMgzp4l4FH7Ah w5bxkSg+USeXZjGQwOMI7gq7cGlPGAw5ETb6EXC+TdrtAO0NPzB8iYACzf8rWZsYl3kT X3kxyeDsNPs29wddhW+AwnKm8QC1ah4O4lA1Gy3WXtBmQu/joWLX/ARlmvmunCOhQ49/ 3+e4eTO1l2cG6yl6RMD77PFhK4W4AV9AGaExvy7zeuHVAtddVaa2q/bzRSDZa35t84hA FeJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uG2nqozuwo9BwCrw+RwCa9b6zn63vZVGMltNLgbwu2E=; b=dzbNaAL615f5cupLOnuIlkEPnO/dbdYt/mj4VX4gIRYPiHLODdBQr1xgITNvm5thZn GIRj1ffUE3jOJDNrLHnWpy00k4nXwOcOMUERkM7EiodgCJ9ZV8/N9mDqvUuNd0VEDvSi EHxU7+Dc4lKWco/+jw3y/K7KTHTVSRqGhwJNyMwj8NRFpIWJ4iRAq5X7cXJb5uNQzBmO t78TMDwGN6kcgCAvKYpPhniiKyi3bWBjmWMg97xZUK+qjrgoBZJJtgJg6/yirvXlXIs3 MID821hNGLMxmslDX6SYYVCdsoDfgIrGqUyuFk50zjJuhlmK4tLRb/kW8ju3fCOPHTni 4s8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZexQjMb8; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id b5sor612444pgc.74.2019.10.02.16.16.26 for (Google Transport Security); Wed, 02 Oct 2019 16:16:26 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZexQjMb8; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uG2nqozuwo9BwCrw+RwCa9b6zn63vZVGMltNLgbwu2E=; b=ZexQjMb8Ay+ScAsdYaEJSxUduZsElYFnqjVAM6juLiP6Fsox2O9LsZ49VUNprld79k yBP6K7pziYnfii5w8mzqklI5RKVYedOPVgM7xovliKQz1keF3nTrVi+GR+05FrApjMAs 9Uw1lPGgfpOdXKbVbu3CLelwgo+zZwuc9wBlA/ui6pFplAY9dX3GuXPymrl2AwaR4Eax yCsfoIiPsy6pkcJRids9x6uVw9zrmTg1TPcEjoAfnz87xXY1xEVxHhTxLGUcb1Xjvv57 v4j0b7HFPL7fag3I61PBGiitShfvcvYx9DeQrv5AMIzbI/SPFMAj5K86qHipeZ6E4wPi naMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uG2nqozuwo9BwCrw+RwCa9b6zn63vZVGMltNLgbwu2E=; b=c1vCAWiijCnezxToFpvIxsoV3s5P2BYdOB2nrPL+ldjQFW+tvWqB0tETs64gHR2p/K MBtf687ki10w6QiYsnZEWRt+PzRCxnUIYqs8A6XJC3wXGIVR0tc2WnMzGXDapUCe7hT9 xOZ8vUYsyqcY/XwpQr0XlL0GXkDZE4fjTQIF3X//zMgtn/+hgYgdSFipzch4F3QiHyTU 3aDarkH43crQseb0d0YGvOmwKhiJrJILKAaTw01fL/KIg6+iQ2WUGsAlQzOMj8YYXGp4 PBLTrcWBcxp/dr+ant8OjGhBXdI+MKTIynhDYP07+kaywtw0oj9JrCvH3SgsBg2YRt3T xLDw== X-Gm-Message-State: APjAAAWiwxpEGQbuv2/g78LTpB6FE8tzLzGemYlROxJcJ96KDJSDioH4 GZ23jzEBBSotznbCh5JxE0ajoD7g X-Google-Smtp-Source: APXvYqxu2u/IQP/LPwB0sAmV4cVg8e5OqNfignEooLRI2THErLoK7DUvR6SLzQPli3BYyhMwbmSigA== X-Received: by 2002:a63:225f:: with SMTP id t31mr6320256pgm.50.1570058185308; Wed, 02 Oct 2019 16:16:25 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:24 -0700 (PDT) From: John Stultz To: lkml Cc: Yu Chen , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, John Stultz Subject: [RFC][PATCH 2/3] usb: roles: Add usb role switch notifier. Date: Wed, 2 Oct 2019 23:16:16 +0000 Message-Id: <20191002231617.3670-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> From: Yu Chen This patch adds notifier for drivers want to be informed of the usb role switch. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Suggested-by: Heikki Krogerus Signed-off-by: Yu Chen Signed-off-by: John Stultz --- drivers/usb/roles/class.c | 35 ++++++++++++++++++++++++++++++++++- include/linux/usb/role.h | 16 ++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index 94b4e7db2b94..418e762d5d72 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -20,6 +20,7 @@ struct usb_role_switch { struct device dev; struct mutex lock; /* device lock*/ enum usb_role role; + struct blocking_notifier_head nh; /* From descriptor */ struct device *usb2_port; @@ -49,8 +50,10 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) mutex_lock(&sw->lock); ret = sw->set(sw->dev.parent, role); - if (!ret) + if (!ret) { sw->role = role; + blocking_notifier_call_chain(&sw->nh, role, NULL); + } mutex_unlock(&sw->lock); @@ -58,6 +61,35 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) } EXPORT_SYMBOL_GPL(usb_role_switch_set_role); +int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + int ret = blocking_notifier_chain_register(&sw->nh, nb); + enum usb_role role; + + if (ret) + return ret; + + /* Initialize the notifier that was just registered */ + mutex_lock(&sw->lock); + if (sw->get) + role = sw->get(sw->dev.parent); + else + role = sw->role; + blocking_notifier_call_chain(&sw->nh, role, NULL); + mutex_unlock(&sw->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier); + +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&sw->nh, nb); +} +EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier); + /** * usb_role_switch_get_role - Get the USB role for a switch * @sw: USB role switch @@ -296,6 +328,7 @@ usb_role_switch_register(struct device *parent, return ERR_PTR(-ENOMEM); mutex_init(&sw->lock); + BLOCKING_INIT_NOTIFIER_HEAD(&sw->nh); sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port; diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h index 2d77f97df72d..8dbf7940b7da 100644 --- a/include/linux/usb/role.h +++ b/include/linux/usb/role.h @@ -54,6 +54,10 @@ struct usb_role_switch * usb_role_switch_register(struct device *parent, const struct usb_role_switch_desc *desc); void usb_role_switch_unregister(struct usb_role_switch *sw); +int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); #else static inline int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) @@ -87,6 +91,18 @@ usb_role_switch_register(struct device *parent, } static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { } + +static int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return -ENODEV; +} + +static int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return -ENODEV; +} #endif #endif /* __LINUX_USB_ROLE_H */ From patchwork Wed Oct 2 23:16:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 175045 Delivered-To: patches@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp1417367ill; Wed, 2 Oct 2019 16:16:27 -0700 (PDT) X-Received: by 2002:a17:90a:ff17:: with SMTP id ce23mr7344677pjb.115.1570058187738; Wed, 02 Oct 2019 16:16:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570058187; cv=none; d=google.com; s=arc-20160816; b=0QLGMzQJWUmjr5zUJ3FdT67WyQCmPXcbAtNIxzijaFpRW/MQrtB1dRakdFBVw+9kzD faOkrqlGY9+/T4/X0XMCnS+ph7wLB3/0eK++5EkOCeasEKXDeq56LAvvQzaCigOcs1Pq UczAh0kXzoDcX46hXYD3v9ZwsaraNxDDqeT1I9CCof/MKZ46TaReWytR2QrmWAddwRJ1 T5WSnkpMxySH9Es65l9vbOpUJUvkui4TGezuVEqlxKqR4zQ8S4O3Z5rFgaDTm7vhjbrM MJz5aOb/iSVOE6C/CqvRry+Y9Qaz75/qzP6Qm7knXFeRU/4Us5ZD83VsXD6W2gu+Sdhz m0Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qxUelUZoTzRMyxNxjTZuYP1rs0YqSW4KUYVQurWum4s=; b=qrCEJibCGRfx5kQTeO8JWLm2hc8Yl0IyMHcCTlpeguLsKY8cWDnOoHKZ+1iKZZHXPz r6h8uQ4SEnKWJVli6sXRv+SYYa8Wi0a2206+hsb2gM1FSv9MrzhreneZrRSm5njzgzi3 +KSHnOaB5L/P592hOboT6/xEunY6W16sTYx7Lt52zgrhQ8HLPHoeIVsm7Aa44n3hneC/ e2fSJGufxfPxXUP7wZolarG0lvp0PWJT596XuUQK0iS8DBSPjrp3z0qTvyZNnlKtIVhJ ieLXITO0Qgypf8Lnmy/bb8IHs8Yx7W1th6YvjbomF5DSKqArVSObL8CB6ajo0wjCSl0H 39yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SEs5WOuM; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id j10sor655260pjn.21.2019.10.02.16.16.27 for (Google Transport Security); Wed, 02 Oct 2019 16:16:27 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SEs5WOuM; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qxUelUZoTzRMyxNxjTZuYP1rs0YqSW4KUYVQurWum4s=; b=SEs5WOuMQ9i46KZODDHA2eT1753KTFIhfQtIBC0vgfTge55jTvENz2ehdd4XniMNpV FpJtfOyUH9JpzGyCOHvO1r+3/4sI8HYLS3DFBp3LIw7XcbjreXvWPVUDbhBEbirCSzEt wCsSGakoroSPK+wb+rVZnLReSg/0yKh333p+YgxAXAeLZ3NtWIG3VPJjTPRBe1rwYhWp qyWb4CTc6qpxj7lv4JpBBjdfFJtbV2Mf93hhOTNY5A/rA/yu+xv5sG0AIv2yzsWMkakG EcVoZFuOtv5RpH1aJLeWXuYiVHwxSUpcaIRXZ6I/TTaJfCj0uDQfK3AzpaJkExFZIExc JmaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qxUelUZoTzRMyxNxjTZuYP1rs0YqSW4KUYVQurWum4s=; b=r8HRqdK4iVMj7OwfkAA/1RcRiEEY/GNOfngkVHXqZV+Tb8vWsAehKdn+nHNFqtZXv9 pU2ye4VQqXQTk9VQ4nr2d73+eQ01U9TKTbRc8+2hVOJNUO1HNOrkrep7uEP9+sZyj/QW FRugtz+/CdYREnko2HPnFgsu58wUBv4i5GGwfv92JJWxVMvXgdtgMIGPMOBOjzFCU95w xXVOfoZsr5a+s/kQxf0K6cDOcixXFKZcpgdjJaRdQryM9vStRy/8ruabDKWS2/OAPvCQ qVR2No/QtiSsF30ZxZKpn4I1j0xbD/8K7K91+xwsR80AWu0t6wwEWbjRiikISRGYS/1f Criw== X-Gm-Message-State: APjAAAWO7okMRC/xib8VSe14+BTPYUQa/PZStTTwJwaBN5FQhhIWPzMd BbGS1o2Imby/VOGsBOv2m0ppcFMB X-Google-Smtp-Source: APXvYqziu9jZ7DI+pIb89nbIQdGRbeE9ytAfHCKWE3r1VSYxVgY8EoqtYas3YrMcVkRMQo60mB2rDA== X-Received: by 2002:a17:90a:8087:: with SMTP id c7mr7238620pjn.56.1570058187290; Wed, 02 Oct 2019 16:16:27 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:26 -0700 (PDT) From: John Stultz To: lkml Cc: Yu Chen , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, John Stultz Subject: [RFC][PATCH 3/3] usb: dwc3: Registering a role switch in the DRD code. Date: Wed, 2 Oct 2019 23:16:17 +0000 Message-Id: <20191002231617.3670-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> From: Yu Chen The Type-C drivers use USB role switch API to inform the system about the negotiated data role, so registering a role switch in the DRD code in order to support platforms with USB Type-C connectors. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Suggested-by: Heikki Krogerus Signed-off-by: Yu Chen Signed-off-by: John Stultz --- drivers/usb/dwc3/Kconfig | 1 + drivers/usb/dwc3/core.h | 6 ++++ drivers/usb/dwc3/drd.c | 78 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig index 89abc6078703..1104745c41a9 100644 --- a/drivers/usb/dwc3/Kconfig +++ b/drivers/usb/dwc3/Kconfig @@ -44,6 +44,7 @@ config USB_DWC3_DUAL_ROLE bool "Dual Role mode" depends on ((USB=y || USB=USB_DWC3) && (USB_GADGET=y || USB_GADGET=USB_DWC3)) depends on (EXTCON=y || EXTCON=USB_DWC3) + select USB_ROLE_SWITCH help This is the default mode of working of DWC3 controller where both host and gadget features are enabled. diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index b3cb6eec3f8f..83728157b3e9 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -951,6 +952,9 @@ struct dwc3_scratchpad_array { * @hsphy_mode: UTMI phy mode, one of following: * - USBPHY_INTERFACE_MODE_UTMI * - USBPHY_INTERFACE_MODE_UTMIW + * role_sw: usb_role_switch handle + * role_switch_default_mode: default operation mode of controller while + * usb role is USB_ROLE_NONE. * @usb2_phy: pointer to USB2 PHY * @usb3_phy: pointer to USB3 PHY * @usb2_generic_phy: pointer to USB2 PHY @@ -1085,6 +1089,8 @@ struct dwc3 { struct extcon_dev *edev; struct notifier_block edev_nb; enum usb_phy_interface hsphy_mode; + struct usb_role_switch *role_sw; + enum usb_dr_mode role_switch_default_mode; u32 fladj; u32 irq_gadget; diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index 726100d1ac0d..95b466a7faa0 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -479,6 +479,58 @@ static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc) return edev; } +static int dwc3_usb_role_switch_set(struct device *dev, enum usb_role role) +{ + struct dwc3 *dwc = dev_get_drvdata(dev); + u32 mode; + + switch (role) { + case USB_ROLE_HOST: + mode = DWC3_GCTL_PRTCAP_HOST; + break; + case USB_ROLE_DEVICE: + mode = DWC3_GCTL_PRTCAP_DEVICE; + break; + default: + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + mode = DWC3_GCTL_PRTCAP_HOST; + else + mode = DWC3_GCTL_PRTCAP_DEVICE; + break; + } + + dwc3_set_mode(dwc, mode); + return 0; +} + +static enum usb_role dwc3_usb_role_switch_get(struct device *dev) +{ + struct dwc3 *dwc = dev_get_drvdata(dev); + unsigned long flags; + enum usb_role role; + + spin_lock_irqsave(&dwc->lock, flags); + switch (dwc->current_dr_role) { + case DWC3_GCTL_PRTCAP_HOST: + role = USB_ROLE_HOST; + break; + case DWC3_GCTL_PRTCAP_DEVICE: + role = USB_ROLE_DEVICE; + break; + case DWC3_GCTL_PRTCAP_OTG: + role = dwc->current_otg_role; + break; + default: + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + role = USB_ROLE_HOST; + else + role = USB_ROLE_DEVICE; + break; + } + spin_unlock_irqrestore(&dwc->lock, flags); + return role; +} + int dwc3_drd_init(struct dwc3 *dwc) { int ret, irq; @@ -487,7 +539,28 @@ int dwc3_drd_init(struct dwc3 *dwc) if (IS_ERR(dwc->edev)) return PTR_ERR(dwc->edev); - if (dwc->edev) { + if (device_property_read_bool(dwc->dev, "usb-role-switch")) { + struct usb_role_switch_desc dwc3_role_switch = {0}; + u32 mode; + + if (device_property_read_bool(dwc->dev, + "role-switch-default-host")) { + dwc->role_switch_default_mode = USB_DR_MODE_HOST; + mode = DWC3_GCTL_PRTCAP_HOST; + } else { + dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL; + mode = DWC3_GCTL_PRTCAP_DEVICE; + } + dwc3_role_switch.fwnode = dev_fwnode(dwc->dev); + dwc3_role_switch.set = dwc3_usb_role_switch_set; + dwc3_role_switch.get = dwc3_usb_role_switch_get; + dwc->role_sw = usb_role_switch_register(dwc->dev, + &dwc3_role_switch); + if (IS_ERR(dwc->role_sw)) + return PTR_ERR(dwc->role_sw); + + dwc3_set_mode(dwc, mode); + } else if (dwc->edev) { dwc->edev_nb.notifier_call = dwc3_drd_notifier; ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->edev_nb); @@ -534,6 +607,9 @@ void dwc3_drd_exit(struct dwc3 *dwc) { unsigned long flags; + if (dwc->role_sw) + usb_role_switch_unregister(dwc->role_sw); + if (dwc->edev) extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->edev_nb);