Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6490637imu; Sun, 2 Dec 2018 19:46:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/XFgZIUxnrzMGIHGLCUleB71EURTssEqYdFc7AEQT7n2uQ5nNhe8U+Qx519GZghKvZWy5dB X-Received: by 2002:a63:ac1a:: with SMTP id v26mr12168807pge.293.1543808802611; Sun, 02 Dec 2018 19:46:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543808802; cv=none; d=google.com; s=arc-20160816; b=Iuj9jpPe4TRCQ2mUTSKj1ReXsq0jhRXWor5ApNBDAv37YTxHdlNMIDMWkfvOL23u24 PfDxMek7TCNcW3iHq7YDWB9w1h0rhwhkUwGQc69/GFOzHR0SRz9G+Vz3EQKNBg6oIfh6 8gMWc+FyPZadqyD34t3+oS+1xQpRhehqaAsZm8bUPSd5xsVsHuaNye9eNN9feKbOHa57 1sShbA9qmunu14Ga/AK5rvSBw65tWQkAc9EIx1yl/WY5fU/OYFDGdDy3N95DtpRf51jq CGUJ4g5ueEGBxO0VDQs5Zfox31g4ukmOpiw2E1tqU13KxqpShS7k/L8wYR+/yxVjxFiU B4nQ== 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=Mu+GBZ3TFOuRCpl3Exv8dNKahl+847uOfduYOpbUutc=; b=bujlh03HNHTK56A/15bztGB49/t06RZOA8b2Gdzmnm/oXwqvCG0iXrE/E2GS4tcsws O81CQiF40glVQ2+FAi0XuqBR0KJw5bHZWJR212rJjyAr7m8BV4MtiW/l8z+YCeBQShP/ UkJyRFwOPTh1dkRdouP9yf60WmE4i2pF4jjV6++9xIwE7o6nEALI3Fk2Fk3xVk+Wx1Aq J60VJNJGdTVRAYSVs4AwgBcXz5AkCPD7gOeyWJshlfCh/U0TYqsaZ3E/JD6DS13PTFm+ TCKBe5SxxOFtKbHRgv7GXbwiQnspG/ozAEb9kJMbqXJNM57u3IRP+vMWA/z8xnkRzjgf n9kw== 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 e9si12202528plt.330.2018.12.02.19.46.28; Sun, 02 Dec 2018 19:46:42 -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 S1726054AbeLCDpu (ORCPT + 99 others); Sun, 2 Dec 2018 22:45:50 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:15627 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725789AbeLCDpt (ORCPT ); Sun, 2 Dec 2018 22:45:49 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 73E36384591E0; 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 08/12] usb: roles: Add usb role switch notifier. Date: Mon, 3 Dec 2018 11:45:11 +0800 Message-ID: <20181203034515.91412-9-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 notifier for drivers want to be informed of the usb role switch. Cc: Greg Kroah-Hartman Cc: Heikki Krogerus Cc: Hans de Goede Cc: Andy Shevchenko Cc: John Stultz Signed-off-by: Heikki Krogerus Signed-off-by: Yu Chen -- v0: The patch is provided by Heikki Krogerus. I modified and test it on Hikey960 platform. -- --- drivers/usb/common/roles.c | 20 +++++++++++++++++++- include/linux/usb/role.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/drivers/usb/common/roles.c b/drivers/usb/common/roles.c index 0f48090c5c30..bfd7a988b64c 100644 --- a/drivers/usb/common/roles.c +++ b/drivers/usb/common/roles.c @@ -21,6 +21,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; @@ -50,8 +51,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); @@ -59,6 +62,20 @@ 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) +{ + return blocking_notifier_chain_register(&sw->nh, nb); +} +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 @@ -297,6 +314,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 edc51be4a77c..c97d4be91ada 100644 --- a/include/linux/usb/role.h +++ b/include/linux/usb/role.h @@ -40,6 +40,8 @@ struct usb_role_switch_desc { bool allow_userspace_control; }; + +#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH) int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role); enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw); struct usb_role_switch *usb_role_switch_get(struct device *dev); @@ -49,5 +51,49 @@ 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) +{ + return 0; +} + +static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw) +{ + return USB_ROLE_NONE; +} + +static inline struct usb_role_switch *usb_role_switch_get(struct device *dev) +{ + return ERR_PTR(-ENODEV); +} + +static inline void usb_role_switch_put(struct usb_role_switch *sw) { } + +static inline struct usb_role_switch * +usb_role_switch_register(struct device *parent, + const struct usb_role_switch_desc *desc) +{ + return ERR_PTR(-ENODEV); +} + +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 */ -- 2.15.0-rc2