c028ba8029ad7b68969763adf726161e6b16066b
[platform/kernel/linux-rpi.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 };
15
16 typedef int (*usb_role_switch_set_t)(struct usb_role_switch *sw,
17                                      enum usb_role role);
18 typedef enum usb_role (*usb_role_switch_get_t)(struct usb_role_switch *sw);
19
20 /**
21  * struct usb_role_switch_desc - USB Role Switch Descriptor
22  * @fwnode: The device node to be associated with the role switch
23  * @usb2_port: Optional reference to the host controller port device (USB2)
24  * @usb3_port: Optional reference to the host controller port device (USB3)
25  * @udc: Optional reference to the peripheral controller device
26  * @set: Callback for setting the role
27  * @get: Callback for getting the role (optional)
28  * @allow_userspace_control: If true userspace may change the role through sysfs
29  * @driver_data: Private data pointer
30  *
31  * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
32  * device controller behind the USB connector with the role switch. If
33  * @usb2_port, @usb3_port and @udc are included in the description, the
34  * reference count for them should be incremented by the caller of
35  * usb_role_switch_register() before registering the switch.
36  */
37 struct usb_role_switch_desc {
38         struct fwnode_handle *fwnode;
39         struct device *usb2_port;
40         struct device *usb3_port;
41         struct device *udc;
42         usb_role_switch_set_t set;
43         usb_role_switch_get_t get;
44         bool allow_userspace_control;
45         void *driver_data;
46 };
47
48
49 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
50 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
51 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
52 struct usb_role_switch *usb_role_switch_get(struct device *dev);
53 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
54 void usb_role_switch_put(struct usb_role_switch *sw);
55
56 struct usb_role_switch *
57 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
58
59 struct usb_role_switch *
60 usb_role_switch_register(struct device *parent,
61                          const struct usb_role_switch_desc *desc);
62 void usb_role_switch_unregister(struct usb_role_switch *sw);
63
64 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
65 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
66 #else
67 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
68                 enum usb_role role)
69 {
70         return 0;
71 }
72
73 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
74 {
75         return USB_ROLE_NONE;
76 }
77
78 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
79 {
80         return ERR_PTR(-ENODEV);
81 }
82
83 static inline struct usb_role_switch *
84 fwnode_usb_role_switch_get(struct fwnode_handle *node)
85 {
86         return ERR_PTR(-ENODEV);
87 }
88
89 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
90
91 static inline struct usb_role_switch *
92 usb_role_switch_register(struct device *parent,
93                          const struct usb_role_switch_desc *desc)
94 {
95         return ERR_PTR(-ENODEV);
96 }
97
98 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
99
100 static inline void
101 usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
102 {
103 }
104
105 static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
106 {
107         return NULL;
108 }
109
110 #endif
111
112 #endif /* __LINUX_USB_ROLE_H */