usb: common: usb-conn-gpio: Set last role to unknown before initial detection
[platform/kernel/linux-starfive.git] / include / linux / usb / role.h
1 // SPDX-License-Identifier: GPL-2.0
2
3 #ifndef __LINUX_USB_ROLE_H
4 #define __LINUX_USB_ROLE_H
5
6 #include <linux/device.h>
7
8 struct usb_role_switch;
9
10 enum usb_role {
11         USB_ROLE_NONE,
12         USB_ROLE_HOST,
13         USB_ROLE_DEVICE,
14         USB_ROLE_UNKNOWN,
15 };
16
17 typedef int (*usb_role_switch_set_t)(struct usb_role_switch *sw,
18                                      enum usb_role role);
19 typedef enum usb_role (*usb_role_switch_get_t)(struct usb_role_switch *sw);
20
21 /**
22  * struct usb_role_switch_desc - USB Role Switch Descriptor
23  * @fwnode: The device node to be associated with the role switch
24  * @usb2_port: Optional reference to the host controller port device (USB2)
25  * @usb3_port: Optional reference to the host controller port device (USB3)
26  * @udc: Optional reference to the peripheral controller device
27  * @set: Callback for setting the role
28  * @get: Callback for getting the role (optional)
29  * @allow_userspace_control: If true userspace may change the role through sysfs
30  * @driver_data: Private data pointer
31  * @name: Name for the switch (optional)
32  *
33  * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
34  * device controller behind the USB connector with the role switch. If
35  * @usb2_port, @usb3_port and @udc are included in the description, the
36  * reference count for them should be incremented by the caller of
37  * usb_role_switch_register() before registering the switch.
38  */
39 struct usb_role_switch_desc {
40         struct fwnode_handle *fwnode;
41         struct device *usb2_port;
42         struct device *usb3_port;
43         struct device *udc;
44         usb_role_switch_set_t set;
45         usb_role_switch_get_t get;
46         bool allow_userspace_control;
47         void *driver_data;
48         const char *name;
49 };
50
51
52 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
53 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
54 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
55 struct usb_role_switch *usb_role_switch_get(struct device *dev);
56 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
57 void usb_role_switch_put(struct usb_role_switch *sw);
58
59 struct usb_role_switch *
60 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
61
62 struct usb_role_switch *
63 usb_role_switch_register(struct device *parent,
64                          const struct usb_role_switch_desc *desc);
65 void usb_role_switch_unregister(struct usb_role_switch *sw);
66
67 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
68 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
69 const char *usb_role_string(enum usb_role role);
70 #else
71 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
72                 enum usb_role role)
73 {
74         return 0;
75 }
76
77 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
78 {
79         return USB_ROLE_NONE;
80 }
81
82 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
83 {
84         return ERR_PTR(-ENODEV);
85 }
86
87 static inline struct usb_role_switch *
88 fwnode_usb_role_switch_get(struct fwnode_handle *node)
89 {
90         return ERR_PTR(-ENODEV);
91 }
92
93 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
94
95 static inline struct usb_role_switch *
96 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
97 {
98         return NULL;
99 }
100
101 static inline struct usb_role_switch *
102 usb_role_switch_register(struct device *parent,
103                          const struct usb_role_switch_desc *desc)
104 {
105         return ERR_PTR(-ENODEV);
106 }
107
108 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
109
110 static inline void
111 usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
112 {
113 }
114
115 static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
116 {
117         return NULL;
118 }
119
120 static inline const char *usb_role_string(enum usb_role role)
121 {
122         return "unknown";
123 }
124
125 #endif
126
127 #endif /* __LINUX_USB_ROLE_H */