Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp178944yba; Fri, 19 Apr 2019 23:42:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqxxa4sgP9v8iGKunRatf6+vJ8RXacOGihdgjZOFX2SrzcznRxiyNTtacssQ4oKGLd3oSA/K X-Received: by 2002:a62:305:: with SMTP id 5mr8531947pfd.65.1555742572176; Fri, 19 Apr 2019 23:42:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555742572; cv=none; d=google.com; s=arc-20160816; b=ogkcD0Z5NLhK/DURbOqWCXkExNd/TAimHGwdxt/kCBaTUA7rAXXQv+dx81tnaqoKo1 yP5SIyUbQZVUDqfNPD77M6ELfge4CSHFheK6qEMX49OPW4nyS0L/D5PTnJTSYcEoiBpA S996yJC4IWg5OqVGwys/vXtl87FJCzp6mYokJ9hVzFsnMGMES/NpXZ/TDwfEygUQdekA FycU/c3azUv6ax79NaG836AVWQQfFm6AFdTB5J+dTCldgtothNgVfiS+gX7Hhx/heUj5 25/IK/9TCBPZppET3tYeS/g28DttqNT1ZXu6MnCjvW3k7GOMM/PP7IH4v00AvAFe9DgQ w9Wg== 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=ypQDF5A3cyRs9guA0loO0aJk5uts/duQAlNylBKXUyI=; b=MnXeu+HpfOvtOEbi2hCwKjJFvVJpfrKoQXxzH/hnZ5AxlSqr/OzrAcBMVzTx/EhOfx yPZngDzlxDv7mmUZvHBeYa8CLJhpQXF7q30K+O52PUO30jEXi0wR7HzsaLvGr5OeFipr nuh25VsPPOaf94qkCpkZM3C6VbnyyW17ZxzyBnCa65e1boXEDLvwS6JsI03emsvRfnC0 vfY9AGZBsWYp8QKhCRZEaCD5apdbNKt+4bHN4tK0zGBWLmhNuMSWgQ2FkZB7/ALAy/1d GYm5liCoST/YAYS7kExlVkqYdOyxLO6mXuHGdrU/zR98jr227YEEXvEsDE7cx2jAPi8r 0oRQ== 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 a2si6562914pgw.545.2019.04.19.23.42.37; Fri, 19 Apr 2019 23:42:52 -0700 (PDT) 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 S1728305AbfDTGlW (ORCPT + 99 others); Sat, 20 Apr 2019 02:41:22 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:6668 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727273AbfDTGkm (ORCPT ); Sat, 20 Apr 2019 02:40:42 -0400 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 52624E6F668A6B2B6076; Sat, 20 Apr 2019 14:40:39 +0800 (CST) Received: from vm100-107-113-134.huawei.com (100.107.113.134) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.408.0; Sat, 20 Apr 2019 14:40:31 +0800 From: Yu Chen To: , , CC: , , , , , , , , , , , , , , Yu Chen , "Greg Kroah-Hartman" , Heikki Krogerus , Hans de Goede , "Andy Shevchenko" Subject: [PATCH v6 09/13] usb: roles: Add usb role switch notifier. Date: Sat, 20 Apr 2019 14:40:15 +0800 Message-ID: <20190420064019.57522-10-chenyu56@huawei.com> X-Mailer: git-send-email 2.15.0-rc2 In-Reply-To: <20190420064019.57522-1-chenyu56@huawei.com> References: <20190420064019.57522-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 Suggested-by: Heikki Krogerus Signed-off-by: Yu Chen --- v5: * Split the patch into two patches, the first one introduces stubs for * the exiting functions, and this patch adds notifier functions. v6: * Fold a fix in form John Stultz . --- --- drivers/usb/roles/class.c | 35 ++++++++++++++++++++++++++++++++++- include/linux/usb/role.h | 16 ++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index f45d8df5cfb8..5bc60f478725 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 @@ -271,6 +303,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 da2b9641b877..99d8b8e4fe61 100644 --- a/include/linux/usb/role.h +++ b/include/linux/usb/role.h @@ -53,6 +53,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) @@ -80,6 +84,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 */ -- 2.15.0-rc2