aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/usb
diff options
context:
space:
mode:
authorRobert Baldyga <r.baldyga@samsung.com>2015-05-04 08:55:11 -0400
committerFelipe Balbi <balbi@ti.com>2015-07-29 10:59:19 -0400
commitccdf138fe3e243c70301fcb6a101e366b7daef07 (patch)
treef1828acbdf9614ca0de4b8e736487c1c9467161d /include/linux/usb
parent351169933ea22592fb42b97c76c4c130fe287cca (diff)
usb: gadget: add usb_gadget_activate/deactivate functions
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 <r.baldyga@samsung.com> Signed-off-by: Felipe Balbi <balbi@ti.com>
Diffstat (limited to 'include/linux/usb')
-rw-r--r--include/linux/usb/gadget.h100
1 files changed, 94 insertions, 6 deletions
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 4f3dfb7d0654..15604bb3e524 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -526,6 +526,9 @@ struct usb_gadget_ops {
526 * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to 526 * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
527 * MaxPacketSize. 527 * MaxPacketSize.
528 * @is_selfpowered: if the gadget is self-powered. 528 * @is_selfpowered: if the gadget is self-powered.
529 * @deactivated: True if gadget is deactivated - in deactivated state it cannot
530 * be connected.
531 * @connected: True if gadget is connected.
529 * 532 *
530 * Gadgets have a mostly-portable "gadget driver" implementing device 533 * Gadgets have a mostly-portable "gadget driver" implementing device
531 * functions, handling all usb configurations and interfaces. Gadget 534 * functions, handling all usb configurations and interfaces. Gadget
@@ -568,6 +571,8 @@ struct usb_gadget {
568 unsigned a_alt_hnp_support:1; 571 unsigned a_alt_hnp_support:1;
569 unsigned quirk_ep_out_aligned_size:1; 572 unsigned quirk_ep_out_aligned_size:1;
570 unsigned is_selfpowered:1; 573 unsigned is_selfpowered:1;
574 unsigned deactivated:1;
575 unsigned connected:1;
571}; 576};
572#define work_to_gadget(w) (container_of((w), struct usb_gadget, work)) 577#define work_to_gadget(w) (container_of((w), struct usb_gadget, work))
573 578
@@ -771,9 +776,24 @@ static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
771 */ 776 */
772static inline int usb_gadget_connect(struct usb_gadget *gadget) 777static inline int usb_gadget_connect(struct usb_gadget *gadget)
773{ 778{
779 int ret;
780
774 if (!gadget->ops->pullup) 781 if (!gadget->ops->pullup)
775 return -EOPNOTSUPP; 782 return -EOPNOTSUPP;
776 return gadget->ops->pullup(gadget, 1); 783
784 if (gadget->deactivated) {
785 /*
786 * If gadget is deactivated we only save new state.
787 * Gadget will be connected automatically after activation.
788 */
789 gadget->connected = true;
790 return 0;
791 }
792
793 ret = gadget->ops->pullup(gadget, 1);
794 if (!ret)
795 gadget->connected = 1;
796 return ret;
777} 797}
778 798
779/** 799/**
@@ -784,20 +804,88 @@ static inline int usb_gadget_connect(struct usb_gadget *gadget)
784 * as a disconnect (when a VBUS session is active). Not all systems 804 * as a disconnect (when a VBUS session is active). Not all systems
785 * support software pullup controls. 805 * support software pullup controls.
786 * 806 *
807 * Returns zero on success, else negative errno.
808 */
809static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
810{
811 int ret;
812
813 if (!gadget->ops->pullup)
814 return -EOPNOTSUPP;
815
816 if (gadget->deactivated) {
817 /*
818 * If gadget is deactivated we only save new state.
819 * Gadget will stay disconnected after activation.
820 */
821 gadget->connected = false;
822 return 0;
823 }
824
825 ret = gadget->ops->pullup(gadget, 0);
826 if (!ret)
827 gadget->connected = 0;
828 return ret;
829}
830
831/**
832 * usb_gadget_deactivate - deactivate function which is not ready to work
833 * @gadget: the peripheral being deactivated
834 *
787 * This routine may be used during the gadget driver bind() call to prevent 835 * This routine may be used during the gadget driver bind() call to prevent
788 * the peripheral from ever being visible to the USB host, unless later 836 * the peripheral from ever being visible to the USB host, unless later
789 * usb_gadget_connect() is called. For example, user mode components may 837 * usb_gadget_activate() is called. For example, user mode components may
790 * need to be activated before the system can talk to hosts. 838 * need to be activated before the system can talk to hosts.
791 * 839 *
792 * Returns zero on success, else negative errno. 840 * Returns zero on success, else negative errno.
793 */ 841 */
794static inline int usb_gadget_disconnect(struct usb_gadget *gadget) 842static inline int usb_gadget_deactivate(struct usb_gadget *gadget)
795{ 843{
796 if (!gadget->ops->pullup) 844 int ret;
797 return -EOPNOTSUPP; 845
798 return gadget->ops->pullup(gadget, 0); 846 if (gadget->deactivated)
847 return 0;
848
849 if (gadget->connected) {
850 ret = usb_gadget_disconnect(gadget);
851 if (ret)
852 return ret;
853 /*
854 * If gadget was being connected before deactivation, we want
855 * to reconnect it in usb_gadget_activate().
856 */
857 gadget->connected = true;
858 }
859 gadget->deactivated = true;
860
861 return 0;
799} 862}
800 863
864/**
865 * usb_gadget_activate - activate function which is not ready to work
866 * @gadget: the peripheral being activated
867 *
868 * This routine activates gadget which was previously deactivated with
869 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
870 *
871 * Returns zero on success, else negative errno.
872 */
873static inline int usb_gadget_activate(struct usb_gadget *gadget)
874{
875 if (!gadget->deactivated)
876 return 0;
877
878 gadget->deactivated = false;
879
880 /*
881 * If gadget has been connected before deactivation, or became connected
882 * while it was being deactivated, we call usb_gadget_connect().
883 */
884 if (gadget->connected)
885 return usb_gadget_connect(gadget);
886
887 return 0;
888}
801 889
802/*-------------------------------------------------------------------------*/ 890/*-------------------------------------------------------------------------*/
803 891