From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754009AbbEDMzy (ORCPT ); Mon, 4 May 2015 08:55:54 -0400 Received: from mailout3.samsung.com ([203.254.224.33]:65480 "EHLO mailout3.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753219AbbEDMzd (ORCPT ); Mon, 4 May 2015 08:55:33 -0400 X-AuditID: cbfee61a-f79516d000006302-d2-55476c42d7d9 From: Robert Baldyga To: balbi@ti.com Cc: gregkh@linuxfoundation.org, m.szyprowski@samsung.com, andrzej.p@samsung.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Robert Baldyga Subject: [PATCH v2 1/5] usb: gadget: add usb_gadget_activate/deactivate functions Date: Mon, 04 May 2015 14:55:11 +0200 Message-id: <1430744115-22096-2-git-send-email-r.baldyga@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1430744115-22096-1-git-send-email-r.baldyga@samsung.com> References: <1430744115-22096-1-git-send-email-r.baldyga@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupjluLIzCtJLcpLzFFi42I5/e+xgK5Tjnuowc/7chazXrazWBy8X2/R vHg9m8XlXXPYLBYta2W2WHvkLrvFg8M72R3YPfbPXcPu0bdlFaPH8RvbmTw+b5ILYInisklJ zcksSy3St0vgyjhw/xhbQY9axdrW08wNjNfluhg5OSQETCQeXpjHCGGLSVy4t56ti5GLQ0hg EaPExzMTmCCcn4wSs3fMYQapYhPQkdjyfQJYh4iAgMT6F5fYQWxmgS2MEp+P6IDYwgLBEtev PGICsVkEVCU6GyeB9fIKuEpMOPSUCWKbnMTJY5NZQWxOATeJ+R/ugM0RAqo5eGEz4wRG3gWM DKsYRVMLkguKk9JzDfWKE3OLS/PS9ZLzczcxgkPpmdQOxpUNFocYBTgYlXh4T6xyCxViTSwr rsw9xCjBwawkwhse4R4qxJuSWFmVWpQfX1Sak1p8iFGag0VJnHeOrlyokEB6YklqdmpqQWoR TJaJg1OqgVE8d7FsUlDn46kvfvafiZ70XfSFI9+Oxs/f/zjsXhH61XZDeY/m42gGlX8hlctn GCY9svo8YYLA/OiGw0dC/C1+Zs2aMfN9dVDQpDWPlesls/NUHdOWcFrOP7SXbX+gczxzn5eU lufJiafsnrY5xWw1mKC75FiByxOjBf+OtTbGfdb+fHKf3kwlluKMREMt5qLiRABGrtwzIQIA AA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org These functions allows to deactivate gadget to make it not visible to host and make it active again when gadget driver is finally ready. They are needed to fix usb_function_activate() and usb_function_deactivate() functions which currently are not working as usb_gadget_connect() is called immediately after function bind regardless to previous calls of usb_gadget_disconnect() function. Signed-off-by: Robert Baldyga --- include/linux/usb/gadget.h | 100 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index 4f3dfb7..15604bb 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -526,6 +526,9 @@ struct usb_gadget_ops { * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to * MaxPacketSize. * @is_selfpowered: if the gadget is self-powered. + * @deactivated: True if gadget is deactivated - in deactivated state it cannot + * be connected. + * @connected: True if gadget is connected. * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget @@ -568,6 +571,8 @@ struct usb_gadget { unsigned a_alt_hnp_support:1; unsigned quirk_ep_out_aligned_size:1; unsigned is_selfpowered:1; + unsigned deactivated:1; + unsigned connected:1; }; #define work_to_gadget(w) (container_of((w), struct usb_gadget, work)) @@ -771,9 +776,24 @@ static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget) */ static inline int usb_gadget_connect(struct usb_gadget *gadget) { + int ret; + if (!gadget->ops->pullup) return -EOPNOTSUPP; - return gadget->ops->pullup(gadget, 1); + + if (gadget->deactivated) { + /* + * If gadget is deactivated we only save new state. + * Gadget will be connected automatically after activation. + */ + gadget->connected = true; + return 0; + } + + ret = gadget->ops->pullup(gadget, 1); + if (!ret) + gadget->connected = 1; + return ret; } /** @@ -784,20 +804,88 @@ static inline int usb_gadget_connect(struct usb_gadget *gadget) * as a disconnect (when a VBUS session is active). Not all systems * support software pullup controls. * + * Returns zero on success, else negative errno. + */ +static inline int usb_gadget_disconnect(struct usb_gadget *gadget) +{ + int ret; + + if (!gadget->ops->pullup) + return -EOPNOTSUPP; + + if (gadget->deactivated) { + /* + * If gadget is deactivated we only save new state. + * Gadget will stay disconnected after activation. + */ + gadget->connected = false; + return 0; + } + + ret = gadget->ops->pullup(gadget, 0); + if (!ret) + gadget->connected = 0; + return ret; +} + +/** + * usb_gadget_deactivate - deactivate function which is not ready to work + * @gadget: the peripheral being deactivated + * * This routine may be used during the gadget driver bind() call to prevent * the peripheral from ever being visible to the USB host, unless later - * usb_gadget_connect() is called. For example, user mode components may + * usb_gadget_activate() is called. For example, user mode components may * need to be activated before the system can talk to hosts. * * Returns zero on success, else negative errno. */ -static inline int usb_gadget_disconnect(struct usb_gadget *gadget) +static inline int usb_gadget_deactivate(struct usb_gadget *gadget) { - if (!gadget->ops->pullup) - return -EOPNOTSUPP; - return gadget->ops->pullup(gadget, 0); + int ret; + + if (gadget->deactivated) + return 0; + + if (gadget->connected) { + ret = usb_gadget_disconnect(gadget); + if (ret) + return ret; + /* + * If gadget was being connected before deactivation, we want + * to reconnect it in usb_gadget_activate(). + */ + gadget->connected = true; + } + gadget->deactivated = true; + + return 0; } +/** + * usb_gadget_activate - activate function which is not ready to work + * @gadget: the peripheral being activated + * + * This routine activates gadget which was previously deactivated with + * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed. + * + * Returns zero on success, else negative errno. + */ +static inline int usb_gadget_activate(struct usb_gadget *gadget) +{ + if (!gadget->deactivated) + return 0; + + gadget->deactivated = false; + + /* + * If gadget has been connected before deactivation, or became connected + * while it was being deactivated, we call usb_gadget_connect(). + */ + if (gadget->connected) + return usb_gadget_connect(gadget); + + return 0; +} /*-------------------------------------------------------------------------*/ -- 1.9.1