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 */